Animating UIButton in Swift

I am making my first game in Swift

and I was wondering how I could animate the buttons at the Z position (or the bottom and top) when the user clicks the button.

Although I found the answer, everything I find is written in Objective-C

, and since I am new to coding, it is very difficult for me to translate Obj-C

into Swift

.

Here's 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];

      

+3


source to share


1 answer


try it

@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/

+8


source







All Articles