重新动画渐变背景与不同的颜色迅速在ios

时间:2016-05-26 21:11:15

标签: ios swift

我希望在制作动画时重新设置不同颜色的渐变背景。 我可以使用此代码成功为渐变颜色设置动画。

let dayTopColor = CommonUtils.colorWithHexString("955EAC")
let dayBottomColor = CommonUtils.colorWithHexString("9F3050")
let dayToTopColor = CommonUtils.colorWithHexString("D15B52")
let dayToBottomColor = CommonUtils.colorWithHexString("CC4645")
let nightTopColor = CommonUtils.colorWithHexString("2D5E7C")
let nightBottomColor = CommonUtils.colorWithHexString("19337D")
let nightToTopColor = CommonUtils.colorWithHexString("21334E")
let nightToBottomColor = CommonUtils.colorWithHexString("101A55")
var isInSaudiArabia = false
var gradient : CAGradientLayer?
var toColors : AnyObject?
var fromColors : AnyObject?

func animateBackground(){
        var layerToRemove: CAGradientLayer?
        for layer in self.view.layer.sublayers!{
            if layer.isKindOfClass(CAGradientLayer) {
                layerToRemove = layer as? CAGradientLayer
            }
        }
        layerToRemove?.removeFromSuperlayer()

            self.gradient!.colors = [nightTopColor.CGColor, nightBottomColor.CGColor]
            self.toColors = [nightToTopColor.CGColor, nightToBottomColor.CGColor]

        self.view.layer.insertSublayer(self.gradient!, atIndex: 0)
        animateLayer()
    }

    func animateLayer(){

        self.fromColors = self.gradient!.colors!
        self.gradient!.colors = self.toColors as? [AnyObject]
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
        animation.delegate = self
        animation.fromValue = fromColors
        animation.toValue = toColors
        animation.duration = 3.50
        animation.removedOnCompletion = true
        animation.fillMode = kCAFillModeForwards
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.delegate = self

        self.gradient!.addAnimation(animation, forKey:"animateGradient")
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

        self.toColors = self.fromColors;
        self.fromColors = self.gradient!.colors!

        animateLayer()
    }

CommonUtils.colorWithHexString()是一个将十六进制颜色转换为UIColor的函数。 顺便说一句,当我尝试在动画时将背景颜色更改为日间颜色时,背景渐变颜色会闪烁。

有没有人知道解决方案。

1 个答案:

答案 0 :(得分:7)

问题在于,当您删除图层时,它会停止动画。但是当动画停止时,animationDidStop仍然被调用,这本身就是一个新的动画。所以,你要删除停止动画的图层,立即启动另一个图层,但是你要开始另一个动画。你有决斗动画。

您可以在flag致电animationDidStop之前检查animateLayer以查看动画是否正常完成。

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    if flag {
        toColors = fromColors;
        fromColors = gradient!.colors!

        animateLayer()
    }
}

就个人而言,我不确定你为什么要删除,添加和删除图层。如果你是,我不确定你为什么不只是gradient?.removeFromSuperlayer()而不是遍历各层。

无论如何,我只保留gradient图层,只需检查其presentationLayer并从那里开始动画:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        fromColors = [dayTopColor.CGColor, dayBottomColor.CGColor]
        toColors = [dayToTopColor.CGColor, dayToBottomColor.CGColor]

        gradient = CAGradientLayer()
        gradient!.colors = fromColors!
        gradient!.frame = view.bounds
        view.layer.addSublayer(gradient!)

        animateLayer()
    }

    let dayTopColor = CommonUtils.colorWithHexString("955EAC")
    let dayBottomColor = CommonUtils.colorWithHexString("9F3050")
    let dayToTopColor = CommonUtils.colorWithHexString("D15B52")
    let dayToBottomColor = CommonUtils.colorWithHexString("CC4645")

    let nightTopColor = CommonUtils.colorWithHexString("2D5E7C")
    let nightBottomColor = CommonUtils.colorWithHexString("19337D")
    let nightToTopColor = CommonUtils.colorWithHexString("21334E")
    let nightToBottomColor = CommonUtils.colorWithHexString("101A55")

    var gradient : CAGradientLayer?
    var toColors : [CGColor]?
    var fromColors : [CGColor]?

    var day = true

    func toggleFromDayToNight() {
        day = !day

        if day {
            fromColors = [dayTopColor.CGColor, dayBottomColor.CGColor]
            toColors = [dayToTopColor.CGColor, dayToBottomColor.CGColor]
        } else {
            fromColors = [nightTopColor.CGColor, nightBottomColor.CGColor]
            toColors = [nightToTopColor.CGColor, nightToBottomColor.CGColor]
        }

        let colors = (gradient!.presentationLayer() as! CAGradientLayer).colors // save the in-flight current colors
        gradient!.removeAnimationForKey("animateGradient")                      // cancel the animation
        gradient!.colors = colors                                               // restore the colors to in-flight values
        animateLayer()                                                          // start animation
    }

    func animateLayer() {
        let animation : CABasicAnimation = CABasicAnimation(keyPath: "colors")
        animation.fromValue = gradient!.colors
        animation.toValue = toColors
        animation.duration = 3.50
        animation.removedOnCompletion = true
        animation.fillMode = kCAFillModeForwards
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.delegate = self

        gradient!.colors = toColors

        gradient!.addAnimation(animation, forKey:"animateGradient")
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if flag {
            swap(&toColors, &fromColors)
            animateLayer()
        }
    }

    @IBAction func didTapButton() {
        toggleFromDayToNight()
    }

}