Animated Images in a UIButton
10,909
You can do this using the imageView property of UIButton
. This will allow the button to behave as it normally would with your images animating on it. Here's an example:
NSMutableArray *imageArray = [NSMutableArray new];
for (int i = 1; i < 4; i ++) {
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
}
[myButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[myButton.imageView setAnimationImages:[imageArray copy]];
[myButton.imageView setAnimationDuration:0.5];
[myButton.imageView startAnimating];
Related videos on Youtube
Author by
Cherr Skees
Updated on June 03, 2022Comments
-
Cherr Skees almost 2 years
What is the best way to animate images of a button?
Say I want to cycle through like 6 frames and have the user still be able to click the button?
Should I animate the images and just have an invisible button on top of it in interface builder?
Is there a way to animate them within defining the
UIButton
?Or is it better to animate images and find the users touch point and act on that?
-
Cherr Skees over 11 yearsOk Good... I didn't know if it would work inside the button like that. Thanks.
-
bobmoff about 10 yearsHow come this work, when myButton.imageView.hidden = YES doesnt ?
-
Mick MacCallum about 10 years@bobmoff Probably because the imageView still exists when you set it to be hidden. All that changes is the image view's alpha.
-
Mick MacCallum about 10 years@bobmoff Sorry, I read that wrong. Are you saying that the buttons image view isn't staying hidden when there's an animation? If so, it sounds like the animation might be unhiding the image view on its own internally.
-
bobmoff about 10 yearsForget the animation. Just simply settings the imageView.hidden = YES or imageView.alpha = 0 doesnt work for me. Is it supposed to work ? The image is still showing. The only way to remove the image is to use the setImage:nil forState: method, but I want to be able to animate the alpha from 1 och 0. I simply want to fade the imageView away.
-
Jignesh B about 10 yearsanimated png also option
-
Mick MacCallum about 10 years@bobmoff I can't reproduce the problem.
-
bobmoff about 10 yearsOk, then I have to be doing something else wrong. Thanks for helping me out. :)