The Dude The Dude - 3 months ago 21
Swift Question

Animating UIButton to zoom in and out on click

I got a button that I've replaced with an icon and when the icon is clicke I want it to zoom in and out for lets say 5 seconds. How can I accomplish this? I have made a set of 5 images with different sizes for the button, can I loop thru theese or is there an other way?

@IBAction func myButton(sender: UIButton){
//animation that zoom the button icon in and out
}


Edit: Im using Xcode 6.4

Answer

This will zoom in and out the button without using additional images:

let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false)
let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(0.25, delay: 0, options: options, 
animations: {
    self.button.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion:nil)

.....

func stopButtonAnimation() {
    button.layer.removeAllAnimations;
}