Jack McBean
Jack McBean

Reputation: 79

How to remove animation by pressing a button. Swift

I have a button and an animation. How can I write in the function (Flower animation) that if the button is pressed, the animation hides or is removed. Any of these options will do.

Anyone has any ideas?

@IBAction func settings(sender: UIButton) {
    settingsPlate.hidden = false
    viewTwo.hidden = false
    exit.hidden = false

    dimmedView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
    dimmedView.alpha = 0.5
    dimmedView.hidden = false

}

This is the Flower animation. I would like to write a code in here that says, if button is pressed go away or hide.

func animateFlowerOne(){

    let options = UIViewAnimationOptions.CurveEaseInOut

    //Flower 1 Four
    let flowers = UIImageView()
    flowers.image = UIImage(named: "flower-face")
    flowers.frame = CGRect(x: 70, y: 380, width: 0, height: 0)
    self.view.addSubview(flowers)


    UIView.animateWithDuration(500.0, delay: 200.0, options: options, animations: {
        flowers.frame = CGRect(x: 70, y: 380, width: 15, height: 15)
        }, completion: { animationFinished in
            UIView.animateWithDuration(500.0, delay: 200.0, options: options, animations: {
                flowers.frame = CGRect(x: 70, y: 380, width: 0, height: 0)

                }, completion: { animationFinished in
                    flowers.removeFromSuperview()
                    self.animateFlowerOne()
            })
    })
}

Upvotes: 4

Views: 5564

Answers (1)

matt
matt

Reputation: 535202

You'll need to keep a reference to the image view that you're animating (e.g. in an instance property of this view controller). If the button is pressed, then in your action handler for the button, use that reference to talk to the image view. You can remove the current animation from the image view's layer with removeAllAnimations, and then you can do whatever else you want to the image view.

It looks like maybe your animation is circular (repeating) so you might also need some kind of instance property that acts as a flag so the animation can check it and stop. Otherwise you'll just repeat forever.

Upvotes: 4

Related Questions