3

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

-(IBAction)

Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mina Dawoud
  • 408
  • 8
  • 22

1 Answers1

8

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;

}
Chris Tetreault
  • 1,973
  • 13
  • 19
  • How could I Put this in a if else statement so when the button is first clicked it moves position, then when clicked again it moves back up – Mina Dawoud Jul 24 '13 at 00:34
  • 1
    If you're just toggling back and forth you could check the buttons frame in your conditional, or set a boolean flag – Chris Tetreault Jul 24 '13 at 00:42
  • Ok...I have no clue how to put this in boolean. Could You please please edit the answer to include it? – Mina Dawoud Jul 24 '13 at 19:05
  • Wow. This is great. But i still get an error when I am trying to initialize boolean to NO. – Mina Dawoud Jul 24 '13 at 19:39
  • Use of unidentified identifier "clicked". This is my first experience with boolean so I appreciate your patience. – Mina Dawoud Jul 24 '13 at 20:27
  • 1
    add `{}` brackets after `yourViewController()` at the top of your .m file and in those brackets put `BOOL clicked;`. Then in viewDidLoad add the line `clicked = NO;` – Chris Tetreault Jul 24 '13 at 20:34
  • 1
    Also Boolean's and boolean logic are very important to programming and development in any language, so I would advise you to read up on that and some other basic stuff before you try and do anything too complex, you'll just keep getting yourself frustrated. :) – Chris Tetreault Jul 24 '13 at 20:38