When I added animation to my button using a category method, then I can't click that button, seems it is disabled:
[_compassCalibrateButton pulse:1.5 continuously:YES];
_compassCalibrateButton.userInteractionEnabled=YES;
I have a UIView category contatining 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];
}
}];
}];
}
I tested on Swift 4
UIView.animate(withDuration: 0.8, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
// Do Something
}, completion: nil)
From the doc
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.
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];
}
}];
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With