我想在 Core Graphics 中绘制一个 UIBerzierPath(例如一个带有 4 个 CGPoints 的矩形)并改变它的位置,也许还有它的大小(例如更大的宽度,更小的高度),这意味着 UIBerzierPath 的四个点改变.
我怎样才能将这种外观变化作为动画(以便点和边移动到新位置),而不是仅仅绘制新的?
最佳答案
创建一个 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)
关于iOS:使用 Core Graphics 绘制 "Movable Polygon",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43681211/