Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add selection animation to a UIButton

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?

like image 991
sergiocg90 Avatar asked Aug 09 '12 19:08

sergiocg90


2 Answers

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];
like image 87
Markus Rautopuro Avatar answered Sep 22 '22 23:09

Markus Rautopuro


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.

like image 31
Bartosz Ciechanowski Avatar answered Sep 24 '22 23:09

Bartosz Ciechanowski