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];
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With