使用 TouchesBegan 停止 viewDidAppear 中的动画 Swift

Using TouchesBegan to stop animation in viewDidAppear Swift

我是 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 做这样的事情。

请帮助和建议我。

非常感谢:]

您可以使用 removeAllAnimations() 从 UI 组件的层中删除当前动画,然后您可以做任何您想做的事情。

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

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

您可以使用 removeAllAnimations 从 UI 组件的层中删除当前动画,然后您可以做任何您想做的事。