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

前端之家收集整理的这篇文章主要介绍了iOS按钮在动画期间无法点击前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用类别方法向我的按钮添加动画时,我无法单击该按钮,似乎它被禁用:
[_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];
                                              }
                                          }];
                     }];
}

解决方法

@H_301_11@ 从文档

During an animation,user interactions are temporarily disabled for
the views being animated. (Prior to iOS 5,user interactions are
disabled for the entire application.) If you want users to be able to
interact with the views,include the
UIViewAnimationOptionAllowUserInteraction constant in the options
parameter.

所以你的代码应该是

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

}
原文链接:https://www.f2er.com/iOS/332732.html

猜你在找的iOS相关文章