The Dude The Dude - 3 months ago 28
Swift Question

removeAllAnimations() ends the button function

With great help from the community I made an zoom in-and-out animation of a button image. The problem is, after the first run the button is disabled and wont work again before the app is relaunched. What I want is that after the button is clicked and the animation is finished it goes back to the starting point, and you can click it again. Anybody here that can help me with that?

@IBAction func coffeeButton(sender: UIButton) {
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false)

let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut

UIView.animateWithDuration(0.5, delay: 0, options: options, animations: {
self.button.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion: nil)
}

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

Answer

Corrected your stopButtonAnimation function.

What applying transform does is setting the scale of your button to it's original value, so you are able to see your animation again.

func stopButtonAnimation(){
    button.layer.removeAllAnimations()
    button.layer.transform = CATransform3DIdentity
}

Animation in your original code was stopped at the scale value of 0.5 so next time you click the button you just don't see it (because it had been animating scale from 0.5 to 0.5).

Comments