快速动画 CAshapeLayer 笔触颜色 [英] Swift-animate CAshapeLayer stroke color

查看:25
本文介绍了快速动画 CAshapeLayer 笔触颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来为我正在创建的笔触颜色设置动画

I'm trying to find a way to animate the color of the stroke that I am creating

circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
circleLayer.lineCap =  kCALineCapRound
circleLayer.fillColor = UIColor.clearColor().CGColor
CABasicAnimation fill = CABasicAnimation.
circleLayer.strokeColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5).CGColor
circleLayer.lineWidth = 20.0;
circleLayer.lineJoin = kCALineJoinRound
// Don't draw the circle initially
circleLayer.strokeEnd = 0.0

// Add the circleLayer to the view's layer's sublayers
layer.addSublayer(circleLayer)

我想要实现的是:在创建它时(我创建它的持续时间为 1 秒),颜色会自行设置动画

What I want to achieve is: while it's being created (I'm creating it over a duration of 1 seconds), the color will animate itself

推荐答案

已更新至 Swift 5.

Updated to Swift 5.

找到解决办法.享受.在创建 CAShapeLayer 当然之后:

Found out the solution. Enjoy. After you create the CAShapeLayer of course :

    let animcolor = CABasicAnimation(keyPath: "strokeColor")
    animcolor.fromValue         = UIColor.green.cgColor
    animcolor.toValue           = UIColor.orange.cgColor
    animcolor.duration          = 1.0
    animcolor.repeatCount       = 0
    animcolor.autoreverses      = true
    myLayer.add(animcolor, forKey: "strokeColor")

这篇关于快速动画 CAshapeLayer 笔触颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆