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

前端之家收集整理的这篇文章主要介绍了重新动画渐变背景与不同的颜色迅速在ios前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在动画时重新设置不同颜色的渐变背景动画.
我可以使用此代码成功为渐变颜色设置动画.
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的函数.
顺便说一句,当我尝试在动画时将背景颜色更改为日间颜色时,背景渐变颜色变得闪烁.

有谁知道解决方案.

解决方法

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

您可以在animationDidStop调用animateLayer之前检查标记以查看动画是否正确完成.

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

        animateLayer()
    }
}

就个人而言,我不确定你为什么要删除,添加删除图层.如果你是,我不确定为什么你不只是渐变?.removeFromSuperlayer()而不是迭代层.

无论如何,我只是将渐变层保留在那里,只需检查其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,finished flag: Bool) {
        if flag {
            swap(&toColors,&fromColors)
            animateLayer()
        }
    }

    @IBAction func didTapButton() {
        toggleFromDayToNight()
    }

}

猜你在找的iOS相关文章