iOS按钮在动画期间无法点击

时间:2013-09-11 13:18:21

标签: ios animation sdk

当我使用类别方法向我的按钮添加动画时,我无法点击该按钮,似乎已被禁用:

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

3 个答案:

答案 0 :(得分:19)

来自文档

  

在动画期间,暂时禁用用户交互   动画的视图。 (在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];
                                              }
                                          }];
                     }];

}

答案 1 :(得分:0)

试试这个会帮到你

- (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;
         }
      }
}

答案 2 :(得分:0)

请注意,如果要为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;}];