Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UIButton down in Swift

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];
like image 909
Casper Avatar asked Dec 26 '22 00:12

Casper


1 Answers

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/

like image 111
rakeshbs Avatar answered Dec 31 '22 15:12

rakeshbs