IOS button not available during animation

When I added animation to my button using the category method, then I can’t click this button, it seems to be disabled:

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

I have a UIView category related to this:

 - (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]; } }]; }]; } 
+6
source share
3 answers

From document

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

So your code should be

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

Try it, it will help you

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

I tested Swift 4

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

Source: https://habr.com/ru/post/953629/


All Articles