Search code examples
swiftanimationtouchesbeganviewdidappear

Using TouchesBegan to stop animation in viewDidAppear Swift


I am new to Swift and I would like to use touchesBegan to stop all animations from viewDidAppear, but it did not work both of remove self.view.layer.removeAllAnimatoins() and speed up animations.

I do this by putting self.view.layer.removeAllAnimatoins() inside of touchesBegan and then set all alpha to normal.

Like this,

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    self.view.layer.removeAllAnimations()

    self.btnO.alpha = 1.0
    self.btnX.alpha = 1.0

    self.titleMultiplayer.alpha = 1.0

    self.questionOX.alpha = 1.0
}  

And this is another code by speeding up animations,

import UIKit
import QuartzCore

class ViewController: UIViewController {

     @IBOutlet var titleMultiplayer: UILabel!

     @IBOutlet var questionOX: UILabel!

     @IBOutlet var btnO: UIButton!
     @IBOutlet var btnX: UIButton!


     override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)

         titleMultiplayer.alpha = 0.0

         questionOX.alpha = 0.0

         btnO.alpha = 0.0
         btnX.alpha = 0.0

     }

     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

         UIView.animateWithDuration(0.001, animations: {

             self.titleMultiplayer.alpha = 1.0

             self.btnO.alpha = 1.0
             self.btnX.alpha = 1.0

             self.questionOX.alpha = 1.0

         })


     }


     override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)



         UIView.animateWithDuration(4.0, delay: 0.0, options: UIViewAnimationOptions.AllowUserInteraction, animations: {

             self.titleMultiplayer.alpha = 1.0

            }, completion: nil)


        UIView.animateWithDuration(4.0, delay: 2.3, options: UIViewAnimationOptions.AllowUserInteraction, animations: {

            self.btnO.alpha = 1.0

            }, completion: nil)

        UIView.animateWithDuration(4.0, delay: 4.3, options: UIViewAnimationOptions.AllowUserInteraction, animations: {

            self.btnX.alpha = 1.0

            }, completion: nil)

        UIView.animateWithDuration(4.0, delay: 6.3, options: UIViewAnimationOptions.AllowUserInteraction, animations: {

            self.questionOX.alpha = 1.0

            }, completion: nil)



     }

     override func viewDidLoad() {
        super.viewDidLoad()



     }

I wonder if it is possible to do something like this with viewDidAppear.

Please help and suggest me.

Thank you very much :]


Solution

  • You can remove the current animation from the UI component's layer with removeAllAnimations(), and then you can do whatever else you want.

    In your case you can remove animation this way in your touchesBegan method:

    btnO.layer.removeAllAnimations()
    btnX.layer.removeAllAnimations()
    titleMultiplayer.layer.removeAllAnimations()
    questionOX.layer.removeAllAnimations()
    

    You can remove the current animation from the UI component's layer with removeAllAnimations, and then you can do whatever else you want.