UIButton resize with animation

10,536

Solution 1

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.

Solution 2

Updated for Swift:

You can insert this in whatever IBAction you've got tied to a button to animate it when it's touched.

    // animate button
    // play with the scale (1.25) to adjust to your liking
    yourButton.transform = CGAffineTransformMakeScale(1.25, 1.25)
    yourButton.alpha = 0.0

    UIView.beginAnimations("yourButton", context: nil)
    // play with the animationDuration to adjust to your liking
    UIView.setAnimationDuration(0.25)
    yourButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
    yourButton.alpha =  1.0
Share:
10,536
Rudiger
Author by

Rudiger

Updated on June 10, 2022

Comments

  • Rudiger
    Rudiger almost 2 years

    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.