简体   繁体   中英

UIButton with UIImageView subview to show animated images

I'm trying to create a button which displays animated images. I read in this post the suggestion that I add an UIImageView (with an array of animationImages) as a subview of the UIButton. I tried to do this like so: (animation1 is a UIImageView object declared in the header)

- (void)viewDidLoad {
 UIImage *image1 = [UIImage imageNamed:@"Good1.png"];
 UIImage *image2 = [UIImage imageNamed:@"Good2.png"];
 UIImage *image3 = [UIImage imageNamed:@"Bad1.png"];
 NSArray *images = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
 animation1.animationImages = images;
 [images release];
 [self.button1 addSubview:animation1];
 [self.button1 bringSubviewToFront:animation1];
 [button1 setNeedsDisplay];
 animation1.animationDuration = 1;
 animation1.animationRepeatCount = 3;
 [animation1 startAnimating];
    [super viewDidLoad];
}

But nothing appears within the button. Any ideas where I might be going wrong?

Also, I would like it so that different methods are called depending on which image is showing when the user presses the button. I achieved this previously but creating an array of images, using an NSTimer to set the background image of the UIButton to cycle to through these images, and then comparing the image data of the button when pressed to the images in the array, calling a different method for each. This really doesn't seem very efficient! Hence why I'm trying to cycle the images in a subview. Any suggestions of how I might extract imagedata from the UIImageView subview of the UIButton when the button is pressed would be most appreciated :)

Thanks!

Michael

This is pure speculation, but sometimes you need to set a frame explicitly. (eg when adding a custom button to a UIBarButtonItem) Try adding a line

animation1.frame = CGRectMake(0,0,50,50);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM