I want to animate an UIBarButtonItem like the TheElements example project does.
I'm pretty sure I've to use the customView property but I don't want to use an image to be able to do that because I need to change titles with some Localized strings (multi language).
So is it possible to create a UIButton which looks like a UIBarButtonItem ?
Here is the code.
NSArray *images = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image0.png"],
[UIImage imageNamed:@"image1.png"],
nil];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image0.png"]];
imageView.animationImages = images;
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = self.imageView.bounds;
[button addSubview:self.imageView];
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * barButton = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];
Some things to notice:
The UIButton is of zero area as it does not have its bounds set upon initialization, thus the bounds are initialized with the bounds of the UIImageView (which has its bounds initialized from the image).
The UIButton handles the action/target for the touch event. The UIBarButtonItem's action/target are not set.
To animate:
[imageView startAnimating];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With