【发布时间】:2018-11-02 09:21:35
【问题描述】:
我可以在时间序列上为曲线的笔划设置动画,但是这条曲线用颜色填充,颜色不会动画,我会看到笔划移动并且填充颜色已经存在。
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = curveFillColor!.cgColor
shapeLayer.strokeColor = curveLineColor!.cgColor
shapeLayer.lineWidth = 3.0
shapeLayer.lineJoin = CAShapeLayerLineJoin.round
shapeLayer.lineCap = CAShapeLayerLineCap.round
shapeLayer.strokeStart = 0
shapeLayer.path = path.cgPath
self.layer.addSublayer(shapeLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1
animation.duration = 4
shapeLayer.add(animation, forKey: "MyAnimation")
我想同时为这条路径制作动画,所以我得到了一条慢慢被颜色填充的曲线。
路径
let path = UIBezierPath()
var point1:CGPoint!
var point2:CGPoint!
//var smoothData = self.smooth(alpha: 0.1)
for i in 0..<curvePoints.count-1
{
point1 = curvePoints[i]
point2 = curvePoints[i+1]
point1.y=size!.height-point1.y
point2.y=size!.height-point2.y
if( i == 0 ) {path.move(to: point1)}
path.addLine(to: point2)
}
编辑: 如果我使用此代码进行动画处理,它不会使用笔划从左到右进行动画处理,只需用颜色填充即可:
let animation = CABasicAnimation(keyPath: "fillColor")
animation.fromValue = UIColor.white.cgColor
animation.toValue = curveFillColor!.cgColor
animation.duration = 4
animation.fillMode = .forwards
animation.isRemovedOnCompletion=false
shapeLayer.add(animation, forKey: "fillColor")
【问题讨论】:
-
我认为您将需要随着时间的推移更改“曲线”的形状来为填充设置动画。由于您的曲线似乎是一系列直线,假设它们从左到右映射,您可以更改路径以对其进行动画处理。
-
调查 developer.apple.com/documentation/quartzcore/cadisplaylink 以帮助随着时间的推移制作动画。
标签: swift uibezierpath cashapelayer