Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset animation after removeAllAnimations()

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()
}
like image 229
The Dude Avatar asked Mar 11 '23 10:03

The Dude


1 Answers

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).

like image 159
Alexander Tkachenko Avatar answered Mar 30 '23 17:03

Alexander Tkachenko