在动画UIButton上进行用户交互 [英] User interaction on an animated UIButton

查看:166
本文介绍了在动画UIButton上进行用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Xcode 4.2针对iPhone的一个小应用程序。我想要的是一个 UIButton 动画向下的屏幕,当你按它,你设置它的alpha为0.我发现一个方法从 UIView 类,它能够处理用户交互,并且来到这个代码:

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

现在,这样做的动画正确,但事情是,它立即设置 origin.y 到300,这意味着我不能按下向下滑动按钮。我只能按下 origin.y 300位置,我可以按它,即使它还没有动画一直在那里。

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.

推荐答案

在视图动画时,触摸事件在iOS中无法正常工作。您可以在视频位于源位置时触摸视图,也可以在视频位于目标位置时触摸视图,但触摸事件不会正确触发视图在动画时的位置。

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

这篇关于在动画UIButton上进行用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆