How to make an interactive UIButton with animated alpha?

I like to create UIButtonone that "breathes", changes its alpha from 1.0 to 0.7 and vice versa, repeating. The code is pretty simple, but when the animation starts, the button is not available. What a pity for the button. I can understand why the button cannot be pressed when the animation changes its position, but changing the alpha seems completely harmless to me. Is there a way to keep the button pressed while keeping the animation and not animating manually?

+3
source share
1 answer

Use opacity.

UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTest.frame = CGRectMake(0, 0, 100, 100);
[btnTest setTitle:@"Breathe" forState:UIControlStateNormal];
[btnTest addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTest];

CABasicAnimation *fadeAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 
fadeAnimation.fromValue=[NSNumber numberWithFloat:0.7];
fadeAnimation.toValue=[NSNumber numberWithFloat:1.0];   
fadeAnimation.duration=1.0;
fadeAnimation.repeatCount=INFINITY;
fadeAnimation.autoreverses=YES;

[btnTest.layer addAnimation:fadeAnimation forKey:@"fadeInOut"];
+10
source

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


All Articles