iOSbutton在animation过程中不可点击

当我使用类别方法向我的button添加animation时,我无法单击该button,似乎它被禁用:

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

我有一个UIView类别contatining这个:

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

从文档

在animation过程中,用户交互暂时停用,以便animation显示视图。 (在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]; } }]; }]; } 

试试这会帮助你

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

我在Swift 4上testing过

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