iOS:使用 Core Graphics 绘制 "Movable Polygon"

标签 ios objective-c swift core-graphics draw

我想在 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/

相关文章:

nsstring - 为什么在转换为 float 或 double 时出现 EXC_BAD_ACCESS 错误? (在 swift 中)

ios - 在实例化 ViewController 之前更改/修改运行时用户定义的属性

iphone - 在通用应用程序中使用 UIPopoverController

iphone - 核心数据获取请求失败,原因为 : 'NSInternalInconsistencyException' ,: 'statement is still active'

objective-c - NSArray 深拷贝导致崩溃

objective-c - 我如何获得 OSX 中的空间数量?

iphone - Objective C 方法命名约定

ios - 如何在通用 Sprite 工具包游戏中调整 Sprite 大小

swift - 如何在 swiftUI 中更改导航标题颜色?

Swift int 构造函数转换十六进制字符串