iOS动画期间按钮无法点击

14

我在按钮上使用了一个分类方法来添加动画效果,结果无法点击该按钮,似乎它已被禁用:

[_compassCalibrateButton pulse:1.5 continuously:YES];
_compassCalibrateButton.userInteractionEnabled=YES;

我有一个UIView类别,其中包含以下内容:

- (void)pulse:(float)secs continuously:(BOOL)continuously {
    [UIView animateWithDuration:secs/2 
                          delay:0.0 
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // Fade out, but not completely
                         self.alpha = 0.3;
                     }
                     completion:^(BOOL finished) { 
                         [UIView animateWithDuration:secs/2 
                                               delay:0.0 
                                             options:UIViewAnimationOptionCurveLinear
                                          animations:^{
                                              // Fade in
                                              self.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished) { 
                                              if (continuously) {
                                                  [self pulse:secs continuously:continuously];
                                              }
                                          }];
                     }];
}
4个回答

29

从文档中得知

在动画期间,被动画处理的视图会暂时禁用用户交互。(在 iOS 5 之前,整个应用程序都会被禁用。)如果你希望用户能够与这些视图进行交互,在选项参数中包含 UIViewAnimationOptionAllowUserInteraction 常数。

因此你的代码应该是

- (void)pulse:(float)secs continuously:(BOOL)continuously {
    [UIView animateWithDuration:secs/2 
                          delay:0.0 
                        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         // Fade out, but not completely
                         self.alpha = 0.3;
                     }
                     completion:^(BOOL finished) { 
                         [UIView animateWithDuration:secs/2 
                                               delay:0.0 
                                             options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                                          animations:^{
                                              // Fade in
                                              self.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished) { 
                                              if (continuously) {
                                                  [self pulse:secs continuously:continuously];
                                              }
                                          }];
                     }];

}

8

我在Swift 4上进行了测试。

 UIView.animate(withDuration: 0.8, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
        // Do Something
    }, completion: nil)

1
尝试这个,它会帮助你:

尝试这个,它会帮助你

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *touch = [touches anyObject];
      CGPoint touchLocation = [touch locationInView:self.view];
      for (UIButton *button in self.arrBtn)
      {
          if ([button.layer.presentationLayer hitTest:touchLocation])
         {
        // This button was hit whilst moving - do something with it here
            [button setBackgroundColor:[UIColor cyanColor]];
             NSLog(@"Button Clicked");
             break;
         }
      }
}

0
请注意,如果您正在对 UIButton 进行动画处理,例如将其在一段时间后逐渐淡出:
[UIView animateWithDuration:1.0 delay:3.0 options:|UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0;
} completion:nil];

无论您设置多长的“延迟”,由于最终阿尔法版本,它都将从一开始就不响应。规避此问题的一种方法是:

[UIView animateWithDuration:1.0 delay:3.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0.1;
} completion:^(BOOL finished){self->btn.alpha = 0;}];

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