当我使用类别方法向我的按钮添加动画时,我无法单击该按钮,似乎它被禁用:
[_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]; } }]; }]; }