Detecting Touches on Animating(Moving) View

282 Views Asked by At

The problem I am having is detecting touches on a UIView or UIViewController that is animating movement across the screen. I have tried UITapGesture, touchesbegan, and UIbutton. But I am finding that the problem is when using an animation block. The Location of the View is set to its end location and cannot be touched while moving. (You can touch the location the view is going to stop at and the touches read). I have tried UIView animation blocks for the animations. I have also tried to use CAKeyframeAnimation but all have the same result.

    [UIView animateWithDuration:10 delay:1 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction) animations:^{
    [testViewController.view setFrame:CGRectMake(900, 20, 100,  100)];

} completion:^(BOOL finished) {
    // Animate moving to another location
}];

Here is a simple example of what I want. I want an Image of a ball randomly moving around the screen. I want to know when the ball is touched.

1

There are 1 best solutions below

0
On BEST ANSWER

I came across the answer by chance on a different project. The problem was that I was trying to find the frame of the view as it was animating. And the frame does not change during the animation. But there is a property of layers that helps.

[view.layer presentationLayer]

you can get the current frame of the animating view from this layer property. Then I can hit test with the presentationLayers frame.