UIButton can#39;t be touched while animated with UIView animateWithDuration(使用 UIView animateWithDuration 进行动画处理时无法触摸 UIButton)
问题描述
I have the following code:
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGRect r = [btn frame];
r.origin.y -= 40;
[btn setFrame: r];
}
completion:^(BOOL done){
if(done){
[UIView animateWithDuration:0.3
delay:1
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGRect r = [btn frame];
r.origin.y += 40;
[btn setFrame: r];
}
completion:^(BOOL done){if(done) zombiePopping = 0; }];
}
}];
The problem is, it seems the button doesnt respond to touches while being animated even though i'm using UIViewAnimationOptionAllowInteraction
, which is a bit weird to me.
Maybe this most be done with Core Animation to work? and if so, how would i go about that?
The touchable part of the button will not coincide with the button's visible frame when it is being animated.
Internally, the button's frame will be set to the final value from your animation. You should find that you can tap in this area, and it would work.
To hit a moving button, you need to do hit testing on the button's .layer.presentationLayer
property (need to import the QuartzCore framework to do this). This would typically be done in touch handling methods in your view controller.
I am happy to expand on this answer if you need more.
Here is how you would respond to a touch event by hit testing presentation layers. This code is in the view controller subclass that is managing your buttons. When I did this I was interested in the initial touch rather than the touch ending (which is typically when a tap would register) but the principle is the same.
Remember to import the QuartzCore framework and add it to your project.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIButton *button in self.buttonsOutletCollection)
{
if ([button.layer.presentationLayer hitTest:touchLocation])
{
// This button was hit whilst moving - do something with it here
break;
}
}
}
这篇关于使用 UIView animateWithDuration 进行动画处理时无法触摸 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UIView animateWithDuration 进行动画处理时无法触摸 UIButton


基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01