使用TouchesBegan在viewDidAppear Swift中停止动画

时间:2015-08-06 06:20:39

标签: swift animation touchesbegan viewdidappear

我是Swift的新手,我想使用touchesBegan停止来自viewDidAppear的所有动画,但它不能同时删除self.view.layer.removeAllAnimatoins()并加快动画效果。

我这样做是将self.view.layer.removeAllAnimatoins()置于touchesBegan内,然后将所有alpha设置为正常。

像这样,

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
}  

这是加速动画的另一个代码,

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



     }

我想知道是否可以使用viewDidAppear执行此类操作。

请帮助并建议我。

非常感谢你:]

1 个答案:

答案 0 :(得分:0)

您可以使用removeAllAnimations()从UI组件的图层中删除当前动画,然后您可以执行任何其他操作。

在您的情况下,您可以在touchesBegan方法中以这种方式删除动画:

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

您可以使用removeAllAnimations从UI组件的图层中删除当前动画,然后您可以执行任何其他操作。