简体   繁体   English

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

[英]How to recognize touch event while a UIButton is animating

I want to move a UIButton with animation, and while its moving if user touches it, I want recognized that event, but during animation UIButton doesn't sent any event. 我想用动画移动一个UIButton,当它移动时如果用户触摸它,我想要识别该事件,但在动画期间UIButton不发送任何事件。

Please solve my problem. 请解决我的问题。

Have you tried AllowUserInteraction option? 你试过AllowUserInteraction选项吗?

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

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. 在动画期间,无论此属性中的值如何,都会暂时禁用动画中涉及的所有视图的用户交互。 You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction option when configuring the animation. 您可以通过在配置动画时指定UIViewAnimationOptionAllowUserInteraction选项来禁用此行为。

Also, you can implement 此外,您可以实施

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

in the superview to catch the event and see if the event is pointing to the button. 在superview中捕获事件并查看事件是否指向按钮。

As UIButton inherit from UIControl which inherit UIView , I believe you are using the block based method to animate your button animateWithDuration:delay:options:animations:completion: 由于UIButton继承自继承UIView UIControl ,我相信您正在使用基于块的方法为您的按钮设置动画animateWithDuration:delay:options:animations:completion:

You will need to add the option UIViewAnimationOptionAllowUserInteraction when you animate your UIButton to allow to recognize some user interaction. 在为UIButton设置动画时,您需要添加选项UIViewAnimationOptionAllowUserInteraction以允许识别某些用户交互。 As you experimented user interaction are by default disabled during the animation otherwise. 在实验中,默认情况下,在动画期间禁用用户交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM