Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I animate a UIButton between two PNG images?

I have two PNGs I want to use as a button. How can I animate a UIButton by switching rapidly between these two images?

like image 225
Jack Avatar asked Sep 20 '11 19:09

Jack


2 Answers

Normally for a button you can set three "live" states: - Normal - Highlighted - Selected I don't know if this can help, but if you set one image to "Normal" and the other image to "Highlighted" you can see the two images while pushing the button. I don't know if this effect is enough for you.

like image 20
viggio24 Avatar answered Nov 14 '22 16:11

viggio24


You can use animationImages property of your button's imageView:

myButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:@"image1.png"],
                          [UIImage imageNamed:@"image2.png"],
                          nil];
myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
[myButton.imageView startAnimating];

Your button will switch between your two images.

EDIT: As @tidbeck pointed the button needs to have an image assigned to create the imageview property.

like image 66
Youssef Avatar answered Nov 14 '22 17:11

Youssef