1
votes

i have a circle image that circles round the screen using a path animation. And i want to detect when the user touches the moving circle. However even though the image is moving round in a continuous circle its frame is still in the top left hand corner not moving, how can i update this so that i can detect a touch on the moving image? Here is the code...

Set Up Animation in ViewDidLoad:

//set up animation
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 10.0;
pathAnimation.repeatCount = 1000;
CGMutablePathRef curvedPath = CGPathCreateMutable();

//path as a circle
CGRect bounds = CGRectMake(60,170,200,200);
CGPathAddEllipseInRect(curvedPath, NULL, bounds);

//tell animation to use this path
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

//add subview
circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];    
[testView addSubview:circleView];

//animate
[circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];

Touches Method:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    //detect touch
    UITouch *theTouch = [touches anyObject];

    //locate and assign touch location
    CGPoint startPoint = [theTouch locationInView:self.view];    
    CGFloat x = startPoint.x;
    CGFloat y = startPoint.y;

    //create touch point
    CGPoint touchPoint = CGPointMake(x, y);

    //check to see if the touch is in the rect    
    if (CGRectContainsPoint(circleView.bounds, touchPoint)) {       

        NSLog(@"yes");
    }

    //check image view position
    NSLog(@"frame x - %f, y - %f", circleView.frame.origin.x, circleView.frame.origin.y);
    NSLog(@"center x - %f, y - %f", circleView.center.x, circleView.center.y);
    NSLog(@"bounds x - %f, y - %f", circleView.bounds.origin.x, circleView.bounds.origin.y);

}

the imageview just seems to stay at the top left hand corner. I cant seem to figure out how to recognise if a touch gesture has been made on the moving ball.

any help would be appreciated,

Chris

1
One of the easiest ways is to create a UIButton of type custom and add your image as the button's background. Then you can add a selector to the button to fire off when it gets touched etc. Hope this helps :) - Luke
Hi, yeah i did try that first of all. But it didn't quite suite what i am after. Because i wanted to use "touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event" to see how long the user can hold the image. Surely there must be a way of accessing the position of the image? Cheers, - ChrisM

1 Answers

1
votes

You need to query the presentation layer of the view not it's frame. Only the presentation will be updated during the course of an animation...

[myImageView.layer presentationLayer]

Access the properties of this layer (origin, size etc) and determine if your touch point is within the bounds.