Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User interaction on an animated UIButton

I am trying to make a little application in Xcode 4.2 targeting the iPhone. What I want is a UIButton that animates down the screen, and when you press it, you set it's alpha to 0. I found a method from 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 does the animations properly, but the thing is that it instantly sets the origin.y to 300, meaning I can't press the button "as it slides down". I can only press it on that origin.y 300 location, and I can press it even though it hasn't animated all the way down there yet.

like image 508
Seerex Avatar asked Oct 25 '11 10:10

Seerex


1 Answers

Touch events do not work correctly in iOS while a view is animating. You can touch views when it sits at the source location, or touch views when it sits at the destination location, but touch events will not trigger correctly for the position the view is at while it is animating.

To deal with this, you must either disable user interaction on the view while it is animating and forget about the idea of touching things while they animate, or you can cover the animating view with another stationary view that has user interaction enabled and thus intercepts the touch events, then tries to figure out if the touch event is in the animating views area. You can do this using:

CGPoint animatingViewPosition = [(CALayer*)[animatingView.layer presentationLayer] position];
like image 78
Kurt Arnlund Avatar answered Sep 18 '22 16:09

Kurt Arnlund