当UIButton正在执行动画时如何识别触摸事件

4
我想要通过动画移动一个UIButton,在移动过程中,如果用户触摸它,我希望能够识别到这个事件,但在动画期间,UIButton不会发送任何事件。
请帮我解决这个问题。

3个回答

8

你尝试过使用AllowUserInteraction选项吗?

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

1
在动画期间,与动画相关的所有视图的用户交互都会被暂时禁用,无论此属性的值如何。您可以通过在配置动画时指定UIViewAnimationOptionAllowUserInteraction选项来禁用此行为。
此外,您还可以实现
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

在父视图中捕获事件并查看事件是否指向按钮。


0

由于UIButton继承自UIControl,而UIControl又继承自UIView,因此我相信您正在使用基于块的方法来动画化您的按钮animateWithDuration:delay:options:animations:completion:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接