Animate UIButton down in Swift

16,793

try this

@IBAction func clicked(sender: AnyObject) {

    button = sender as UIButton

    UIView.animateWithDuration(1.0, animations:{
        self.button.frame = CGRectMake(self.button.frame.origin.x + 25, self.button.frame.origin.y + 25, self.button.frame.size.width, self.button.frame.size.height)
    })

}

for more information read this article http://mathewsanders.com/prototyping-iOS-iPhone-iPad-animations-in-swift/

Share:
16,793
Casper
Author by

Casper

Updated on June 04, 2022

Comments

  • Casper
    Casper almost 2 years

    I'm making my first game in Swift and I was wondering how I could animate the buttons in Z position (or downscale and upscale) when the user presses the button.

    I though that I found the answer but everything I find is written in Objective-C and since I'm new to coding it's pretty hard for me to translate Obj-C to Swift.

    This is what I found:

    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];
    
    • Name
      Name over 9 years
      What's your problem?
    • Casper
      Casper over 9 years
      Well I don't know what it says in Obj-C so I'm looking for someone that could help me translate that to swift so I can implement it in my code.
  • bearacuda13
    bearacuda13 almost 7 years
    oh, sorry for the downvote... I've nested my buttons in a lot of views and figured out your code works fine; stackoverflow won't let me upvote it