我想在Core Graphics中绘制一个UIBerzierPath(例如一个带有4个CGPoints的矩形)并改变它的位置,也可能改变它的大小(例如更大的宽度,更小的高度),这意味着,UIBerzierPath的四个点发生了变化
如何将外观更改为动画(以使点和边移动到新位置),而不仅仅是绘制新的?
答案 0 :(得分:2)
创建CAShapeLayer
let pathLayer = CAShapeLayer()
let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 100, height: 100))
pathLayer.path = path.cgPath
pathLayer.strokeColor = UIColor.red.cgColor
pathLayer.strokeColor = UIColor.red.cgColor
pathLayer.fillColor = UIColor.green.cgColor
pathLayer.fillRule = kCAFillRuleEvenOdd
self.view.layer.addSublayer(pathLayer)
像这样动画:
let animation = CABasicAnimation(keyPath: "path")
animation.toValue = UIBezierPath(rect: CGRect(x: 200, y: 200, width: 100, height: 100)).cgPath
animation.duration = 1
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animation.fillMode = kCAFillModeBoth
animation.isRemovedOnCompletion = false
pathLayer.add(animation, forKey: animation.keyPath)