如何在 UIButton 制作动画时识别触摸事件



我想移动带有动画的UIButton,当用户触摸它时它会移动时,我希望识别该事件,但在动画过程中UIButton不会发送任何事件。

请解决我的问题。

你试过允许用户交互选项吗?

 [UIView animateWithDuration:5.0 
                       delay:0.0         
                     options:UIViewAnimationOptionAllowUserInteraction
                  animations:^{ // move the button }
                 completion:^(BOOL finished){}];

在动画期间,无论此属性中的值如何,都会暂时禁用动画中涉及的所有视图的用户交互。您可以通过在配置动画时指定 UIViewAnimationOptionAllowUserInteraction 选项来禁用此行为。

此外,您可以实现

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

以捕获事件并查看事件是否指向按钮。

由于UIButton继承自继承UIView UIControl,我相信您正在使用基于块的方法对按钮进行动画处理animateWithDuration:delay:options:animations:completion:

当您对 UIButton 进行动画处理时,您需要添加选项UIViewAnimationOptionAllowUserInteraction以允许识别某些用户交互。当您进行实验时,默认情况下,在动画期间禁用用户交互,否则。

最新更新