【问题标题】:Using TouchesBegan to stop animation in viewDidAppear Swift使用 TouchesBegan 在 vi​​ewDidAppear Swift 中停止动画
【发布时间】:2015-08-06 06:20:39
【问题描述】:

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

请帮助并建议我。

非常感谢:]

【问题讨论】:

    标签: swift animation touchesbegan viewdidappear


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2018-02-19
      • 2015-11-30
      • 2019-03-10
      相关资源
      最近更新 更多