Reputation: 8236
I'm trying to do a simple 2 frame animation of a UIButton
. I understand that this is possible using the UIButton's
imageView.animationImages
property - setting it to an array and animating it. But when I do so, nothing appears. FYI, I've checked the buttonImages array and it does indeed contain the images I want to show. Any ideas where I'm going wrong?
NSArray *buttonImages = bookDoc.book.buttonImages;
UIImage *bookImage = bookDoc.thumbImage;
UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];
bookButton.frame = CGRectMake(0, 0, bookImage.size.width, bookImage.size.height);
//[bookButton setBackgroundImage:bookImage forState:UIControlStateNormal];
bookButton.imageView.animationImages = buttonImages;
bookButton.imageView.animationDuration = 0.5;
bookButton.imageView.animationRepeatCount = INFINITY;
[bookButton addTarget:self action:@selector(bookButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[bookButton.imageView startAnimating];
Upvotes: 1
Views: 1888
Reputation: 16338
I just wrote this, it works. The downside of it is that the highlighted image that I set in IB does not seem to work. Xcode 5. Maybe disabled images and so on won't work either. If you don't care for that, this works.
+(void)animatemjad:(UIButton*)button offimagenames:(NSArray*)imagenames startdelay:(const NSTimeInterval)STARTDELAY {
UIImageView* imageview = [button imageView];
NSMutableArray* images = [NSMutableArray array];
for (NSString* imagename in imagenames) {
UIImage* image = [UIImage imageNamed:imagename];
NSAssert(image != nil, @"Failed loading imagename: %@", imagename);
if (image == nil)
return; // return out here... whatever
[images addObject:image];
}
imageview = [[UIImageView alloc] initWithImage:images.firstObject];
[button addSubview:imageview];
imageview.animationImages = images;
imageview.animationDuration = 5;
imageview.animationRepeatCount = 0;
[imageview performSelector:@selector(startAnimating) withObject:nil afterDelay:STARTDELAY]; // [imageview startAnimating];
}
Upvotes: 0
Reputation: 90127
The imageView
of the UIButton
is initialized with a frame of (0,0,0,0), i.e. it is not visible (and it's actually hidden too).
You can configure the frame
and the hidden
properties yourself:
bookButton.imageView.frame = CGRectMake(0, 0, 20, 20);
bookButton.imageView.hidden = NO;
Or, even simpler just use the first image of your animation
[bookButton setImage:buttonImages[0] forState:UIControlStateNormal];
This will unhide the imageView and sets its correct frame.
Once the imageView is visible it will animate.
Upvotes: 3
Reputation: 131501
I don't think I've ever tried to save an image to a button's imageView property. I've always used setImage:forState or setBackgroundImage:forState.
My guess is that the button is setting the image property of it's image view itself, using the (nil) image for the default image state.
Have you tried setting the button.imageView.image property to a static image to see if that works? My guess, as I said, is that the button will overwrite any image you provide with the setting provided in setImage:forState (or nil, if you never provided an image for the current state.)
Upvotes: 1