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

4 回答 4

29

从文档

在动画期间,对于正在动画的视图,用户交互暂时被禁用。(在 iOS 5 之前,整个应用程序都禁用了用户交互。)如果您希望用户能够与视图交互,请在 options 参数中包含 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];
                                              }
                                          }];
                     }];

}
于 2013-09-11T13:21:30.323 回答
7

我在 Swift 4 上测试过

 UIView.animate(withDuration: 0.8, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
        // Do Something
    }, completion: nil)
于 2017-11-27T09:20:37.927 回答
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;
         }
      }
}
于 2014-04-04T11:26:39.083 回答
0

请注意,如果您要让 a 动画UIButton在一段时间后淡出,例如:

[UIView animateWithDuration:1.0 delay:3.0 options:|UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0;
} completion:nil];

由于最终的 alpha,无论您的“延迟”如何,它都会从一开始就没有响应。解决这个问题的一种方法是:

[UIView animateWithDuration:1.0 delay:3.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    self->btn.alpha = 0.1;
} completion:^(BOOL finished){self->btn.alpha = 0;}];
于 2020-12-07T09:41:33.107 回答