使用 UIView animateWithDuration 设置动画时无法触摸 UIButton [英] UIButton can't be touched while animated with UIView animateWithDuration

查看:35
本文介绍了使用 UIView animateWithDuration 设置动画时无法触摸 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

                 }];

问题是,即使我使用的是 UIViewAnimationOptionAllowInteraction,按钮在动画时似乎没有响应触摸,这对我来说有点奇怪.

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.

也许这主要是通过 Core Animation 来完成的?如果是这样,我会怎么做?

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.

要点击移动按钮,您需要对按钮的 .layer.presentationLayer 属性进行点击测试(需要导入 QuartzCore 框架来执行此操作).这通常会在您的视图控制器中的触摸处理方法中完成.

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.

记得导入 QuartzCore 框架并将其添加到您的项目中.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆