User interaction with animated UIButton

I am trying to make a small application in Xcode 4.2 for iPhone. What I want is a UIButton that animates around the screen, and when you click it, you set its alpha value to 0. I found a method of the UIView class that was able to deal with user interactions, and came to this code:

  [UIView animateWithDuration:3.0 delay:0 options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent animations: ^{ CGRect myRect = enemy1.frame; myRect.origin.y = 300; myButton.frame = myRect; } completion:nil]; 

Now this makes the animation correct, but the fact is that it instantly sets the value of origin.y to 300, that is, I can’t click the "as it slides" button. I can click it only on this place origin.y 300, and I can click it even if it is not animated so far.

+4
source share
2 answers

Touching events does not work correctly in iOS during animation. You can touch the views when it sits at the source location, or touch the views when it sits at the destination, but touch events will not trigger correctly for the position the view is in while it is being animated.

To deal with this, you must either turn off the user’s interaction with the view while it is being animated, or forget how to touch things during the animation, or you can cover the animating view with another still view that includes user interaction in this way, intercepts touch events, and then tries to find out if the touch event is in the area of ​​animated views. You can do this using:

 CGPoint animatingViewPosition = [(CALayer*)[animatingView.layer presentationLayer] position]; 
+5
source

you can use

 button.frame = CGRectMake(100, 100, 60, 40); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:4.0]; button.frame = CGRectMake(100, 300, 60, 40); [UIView commitAnimations]; 
+1
source

All Articles