I want to animate the transition between normal and highlighted state of a UIButton.
I put 2 images as background of the two states in IB and wrote the following code for TouchUpInside event:
- (void) animate {
[UIView transitionWithView:self.myButton
duration:2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ self.myButton.highlighted = YES; }
completion:^(BOOL finished) {[UIView transitionWithView:self.myButton
duration:2
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ self.myButton.highlighted = NO; }
completion:nil];}
];
}
In this way the animation is not started regularly and in particular if I tap quickly on the button.
I found that by preceding the above method with the following (linked to the UIButton in place of the previous one) the animation works without any problem ... but I don't understand the reason !
Could someone explain what exactly happens when I add this last method ?
- (void) firstThis {
[self performSelector: @selector(animate) withObject: nil afterDelay: 0];
}
Thanks, Corrado
performSelector always schedules your method to be performed later (in this case on main), even if you set delay = 0. So, in your case the system gets a chance to properly start/stop animations and/or finish other stuff, like for example: the automatic highlighting of the button that happens because of touch-down events. The system starts the animation after having finished its own highlight animation of the button. The direct call interferes with this.