停止旧动画并开始新动画,一旦我更改片段,swift

Stop old animation and start new one, once I change segments, swift

有什么方法或函数可以在我切换段控件时停止之前的动画吗? 如果我不删除动画,则在第一段褪色的图像在第二段开始旋转,然后在第三段移动。 当我切换片段时,它更像是一直附加不同的动画。

我试过: - removeAllAnimations(),但它不能正常工作。 - self.view.layer.removeAnimation(forKey: "moveAnimation")。在每个段选择的代码末尾尝试了那个。

也许我没有把它放在正确的位置,但我不能让它正常工作

这是我的代码:

import UIKit

class MoveViewController: UIViewController {


    @IBOutlet var sgAction : UISegmentedControl!

    var moveLayer : CALayer?

    @IBAction func segmentDidChange(sender : UISegmentedControl){
        updateAction()

    }


    func updateAction(){
        let action = sgAction.selectedSegmentIndex

        if action == 0 {
            //fade

            let fadeAnimation = CABasicAnimation(keyPath: "opacity")

            self.view.layer.removeAnimation(forKey: "fadeAnimation")

            fadeAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            fadeAnimation.fromValue = NSNumber.init(value: 1.0)
            fadeAnimation.toValue = NSNumber.init(value: 0.0)
            fadeAnimation.isRemovedOnCompletion = false

            fadeAnimation.duration = 3.0
            fadeAnimation.beginTime = 1.0
            fadeAnimation.isAdditive = false
            fadeAnimation.fillMode = CAMediaTimingFillMode.both
            fadeAnimation.repeatCount = Float.infinity
            moveLayer?.add(fadeAnimation, forKey: nil)

        } else if action == 1 {
            //2. rotate

            let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")

            self.view.layer.removeAnimation(forKey: "rotateAnimation")

            rotateAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            rotateAnimation.fromValue = 0
            rotateAnimation.toValue = 2 * Double.pi

            rotateAnimation.isRemovedOnCompletion = false

            rotateAnimation.duration = 1.0
            rotateAnimation.repeatCount = Float.infinity
            moveLayer?.add(rotateAnimation, forKey:nil)


        } else {
            //3. move

            let moveAnimation = CABasicAnimation(keyPath: "position")
            self.view.layer.removeAnimation(forKey: "moveAnimation")

            moveAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)

            moveAnimation.fromValue = NSValue.init(cgPoint: CGPoint(x: 0, y: 0))
            moveAnimation.toValue = NSValue.init(cgPoint: CGPoint(x: 700, y: 500))
            moveAnimation.isRemovedOnCompletion = false
            moveAnimation.duration = 3.0
            moveAnimation.repeatCount = Float.infinity
            moveLayer?.add(moveAnimation, forKey: nil)

        }
    }
//        moveLayer?.removeAllAnimations()




    func createImg(){
        let displayImage = UIImage(named: "balloon.png")
        moveLayer = CALayer.init()
        moveLayer?.contents = displayImage?.cgImage
        moveLayer?.bounds = CGRect(x: 0, y: 0, width: 150, height: 150)
        moveLayer?.position = CGPoint(x: 300, y: 200)
        self.view.layer.addSublayer(moveLayer!)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        createImg()
//      moveLayer?.removeAllAnimations()
        updateAction()

    }

}

当您将 moveLayer?.removeAllAnimations()self.moveLayer.layoutIfNeed() 添加到 segmentDidChange(sender : UISegmentedControl) 时。另一个需要更改的关键项目是 moveLayer?.add(fadeAnimation, forKey: "nil")moveLayer?.add(fadeAnimation, forKey: "fadeAnimation")

import UIKit

class MoveViewController: UIViewController {

    @IBOutlet var sgAction : UISegmentedControl!

    var moveLayer : CALayer?

    override func viewDidLoad() {
        super.viewDidLoad()
        createImg()
        updateAction()
    }

    @IBAction func segmentDidChange(sender : UISegmentedControl){
        moveLayer?.removeAllAnimations()
        self.moveLayer?.layoutIfNeeded()
        updateAction()
    }

    func updateAction(){
        switch sgAction.selectedSegmentIndex {
        case 0:
            //fade
            let fadeAnimation = CABasicAnimation(keyPath: "opacity")

            fadeAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            fadeAnimation.fromValue = NSNumber.init(value: 1.0)
            fadeAnimation.toValue = NSNumber.init(value: 0.0)
            fadeAnimation.isRemovedOnCompletion = false

            fadeAnimation.duration = 3.0
            fadeAnimation.beginTime = 1.0
            fadeAnimation.isAdditive = false
            fadeAnimation.fillMode = CAMediaTimingFillMode.both
            fadeAnimation.repeatCount = Float.infinity
            moveLayer?.add(fadeAnimation, forKey: "fadeAnimation")

        case 1:
            //2. rotate
            let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")

            rotateAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            rotateAnimation.fromValue = 0
            rotateAnimation.toValue = 2 * Double.pi

            rotateAnimation.isRemovedOnCompletion = false

            rotateAnimation.duration = 1.0
            rotateAnimation.repeatCount = Float.infinity
            moveLayer?.add(rotateAnimation, forKey: "rotateAnimation")

        case 2:
            //3. move
            let moveAnimation = CABasicAnimation(keyPath: "position")

            moveAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)

            moveAnimation.fromValue = NSValue.init(cgPoint: CGPoint(x: 0, y: 0))
            moveAnimation.toValue = NSValue.init(cgPoint: CGPoint(x: 700, y: 500))
            moveAnimation.isRemovedOnCompletion = false
            moveAnimation.duration = 3.0
            moveAnimation.repeatCount = Float.infinity
            moveLayer?.add(moveAnimation, forKey: "position")
        default: break

        }
    }

    func createImg(){
        let displayImage = UIImage(named: "balloon.png")
        moveLayer = CALayer.init()
        moveLayer?.contents = displayImage?.cgImage
        moveLayer?.bounds = CGRect(x: 0, y: 0, width: 150, height: 150)
        moveLayer?.position = CGPoint(x: 300, y: 200)
        self.view.layer.addSublayer(moveLayer!)
    }

}