使用UIView animateWithDuration进行动画处理时,无法触摸UIButton

Sha*_*ali 43 iphone core-animation objective-c uiviewanimation ios

我有以下代码:

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

                 }];
Run Code Online (Sandbox Code Playgroud)

问题是,即使我正在使用UIViewAnimationOptionAllowInteraction,按钮也不会在动画时响应触摸,这对我来说有点奇怪.

也许最好用Core Animation来完成这项工作?如果是的话,我该怎么做呢?

jrt*_*ton 53

按钮的可触摸部分在动画时不会与按钮的可见帧重合.

在内部,按钮的框架将设置为动画的最终值.你会发现你可以点击这个区域,它会起作用.

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

如果您需要更多,我很乐意扩展这个答案.

以下是如何通过命中测试表示层来响应触摸事件.此代码位于管理按钮的视图控制器子类中.当我这样做时,我对初始触摸而不是触摸结束感兴趣(这通常是当水龙头会注册时),但原理是相同的.

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

- (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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


nah*_*g89 53

在我的情况下,我设置superView.alpha = 0,它停止按钮的交互,虽然我设置UIViewAnimationOptionAllowUserInteraction.

解?容易,只需减少alpha 到0.1而不是0

[UIView animateWithDuration:durationTime delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionOverrideInheritedOptions | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations: ^{
            superView.alpha = 0.1; // 0.0 will make the button unavailable to touch
        } completion:nil];
Run Code Online (Sandbox Code Playgroud)

  • 绝对是最好的答案. (6认同)
  • 这个小黑客需要100个大拇指.1秒改变,一切正常.真棒 (3认同)
  • Swift 代码 UIView.animate(withDuration: 2.0, delay: 0, options:[UIViewAnimationOptions.repeat, UIViewAnimationOptions.allowUserInteraction], animations: { button.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) }, completion:nil); (2认同)

小智 27

选项的顺序是重要的,您必须先放置UIViewAnimationOptionAllowUserInteraction,然后添加其他选项.

在Swift 3中使用: .allowUserInteraction