用户交互animationUIButton

我试图在Xcode 4.2中针对iPhone做一个小应用程序。 我想要的是一个UIButton ,在屏幕上进行animation处理,当你按下它的时候,你把它设置为0.我从UIView类中find了一个能够处理用户交互的方法,来到这个代码:

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

现在,这样做的animation正确,但事情是,它立即将origin.y设置为300,这意味着我不能按下button“,因为它下滑”。 我只能在origin.y位置上按下它,即使它还没有完成animation,我仍然可以按下它。

触摸事件在iOS中正常工作,而视图是animation。 您可以在视图位于源位置时触摸视图,也可以在视图位于目标位置时触摸视图,但在视图处于animation状态时,触摸事件不会正确触发视图所在的位置。

为了解决这个问题,你必须禁止视图上的用户交互,而不是在animation时触摸事物,或者你可以用启用了用户交互的另一个静止视图覆盖animation视图,从而拦截触摸事件,然后试图找出触摸事件是否在animation视图区域。 你可以这样做:

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

您可以使用,

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