Add selection animation to a UIButton

3.4k views Asked by At

I have a UIButton in one of my ViewControllers with three different state images (normal, highlighted and selected). When I press this button, it becomes highlighted and when I release it, selected. I want to make an animation that changes the highlighted image of the button to the selected one in 4 seconds with a fadeIn or so.

Any suggestion?

2

There are 2 answers

0
Bartosz Ciechanowski On

You have to enroll your own UIButton subclass and you'll probably have to overwrite setSelected and setHighlighted with your custom animation code. The safer bet would be to subclass UIControl directly and reimplement those selectors so that you don't have any UIButton specific legacy.

0
Markus Rautopuro On

Try UIView transitionWithView by setting first the normal and selected images to UIButton, and then in the action handler animate the change:

[UIView transitionWithView:self.addListButton
                  duration:0.3
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    [self.addListButton setSelected:YES];
                } completion:nil];

I didn't want to change the state of the UIButton so I just changed the normal image in the animation block:

[self.addListButton setImage:[UIImage imageNamed:@"icon-addbutton.png"]
    forState:UIControlStateNormal];