Animate UIButton Down - Xcode

2019-04-11 22:51发布

问题:

I wanted to know how I would go about animating a UIButton down when clicked via

-(IBAction)

Thanks in advance!

回答1:

Inside your IBAction

UIButton *button = (UIButton*)sender;

//animates button 25 pixels right and 25 pixels down. Customize
CGRect newFrame = CGRectMake(button.frame.origin.x + 25, button.frame.origin.y + 25, button.frame.size.width, button.frame.size.height);

[UIView animateWithDuration:0.3f
                      delay:0.0f
                    options: UIViewAnimationOptionCurveLinear
                 animations:^{
                     [button setFrame:newFrame];
                 }
                 completion:nil];

EDIT - To toggle between frames

initialize boolean clicked to NO somewhere

if (!clicked){

    //animates button 25 pixels right and 25 pixels down. Customize
    CGRect newFrame = CGRectMake(button.frame.origin.x + 25, button.frame.origin.y + 25, button.frame.size.width, button.frame.size.height);

    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [button setFrame:newFrame];
                     }
                     completion:nil];

     clicked = YES;

} else {


    //animates button 25 pixels left and 25 pixels up. Customize
    CGRect newFrame = CGRectMake(button.frame.origin.x - 25, button.frame.origin.y - 25, button.frame.size.width, button.frame.size.height);

    [UIView animateWithDuration:0.3f
                          delay:0.0f
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [button setFrame:newFrame];
                     }
                     completion:nil];

    clicked = NO;

}