【问题标题】:iOS: Draw "Movable Polygon" with Core GraphicsiOS:使用 Core Graphics 绘制“可移动多边形”
【发布时间】:2017-04-28 13:18:09
【问题描述】:
我想在 Core Graphics 中绘制一个 UIBerzierPath(例如一个具有 4 个 CGPoints 的矩形)并改变它的位置,也许还有它的大小(例如更大的宽度,更小的高度),这意味着 UIBerzierPath 的四个点发生了变化.
我怎样才能将这种外观更改为动画(以便点和边移动到新位置),而不是仅仅绘制新的?
【问题讨论】:
标签:
ios
objective-c
swift
core-graphics
draw
【解决方案1】:
创建一个 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)