User interaction on an animated UIButton

北城以北 提交于 2019-12-22 06:00:05

问题


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.


回答1:


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];



回答2:


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];


来源:https://stackoverflow.com/questions/7888116/user-interaction-on-an-animated-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!