Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton resize with animation

I'm creating a menu and I want the buttons to "pop" I guess on the screen. Basically I want to start at 0px dimensions and then go up to the full size of the buttons. I can animate the alpha and the position if I want to but can't do the dimensions and I think its because its an image on the button.

If I do a UIButtonTypeRoundRect you can see the button being animated behind the image but the image is static.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(20, 20, 0, 0);
button.alpha = 0;
[self.view addSubview:button];
CGRect frame = button.frame;
[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
button.alpha = 1;
frame.size.width += 53;
frame.size.height += 53;
button.frame = frame;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

So the Alpha works but the resize doesn't. I've also played with stretchableImageWithLeftCapWidth to try and give it context or something but to no avail.

Cheers for your help.

like image 820
Rudiger Avatar asked Jun 30 '10 00:06

Rudiger


1 Answers

Could you try using the following code instead?

button.transform = CGAffineTransformMakeScale(1.5,1.5);
button.alpha = 0.0f;

[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
     button.transform = CGAffineTransformMakeScale(1,1);
     button.alpha = 1.0f;
[UIView commitAnimations];

Your button should start slightly larger and then shrink back down. If this scales properly, just adjust the scalefactor before and after to suit.

like image 137
davbryn Avatar answered Oct 02 '22 17:10

davbryn