CGPath 与 UIBezierPath

CGPath vs UIBezierPath

使用以下列方式获取 CGPath 的一段代码:

let dirX = -spread
let rect = bounds.insetBy(dx: dirX, dy: dirX)
let cgPath = UIBezierPath(rect: rect).cgPath

有没有一种方法可以直接使用该矩形来获取 CGPath?

我知道有

CGPath(rect: CGRect, transform: UnsafePointer<CGAffineTransform>)

但是我不确定直接用init获取相同的CGPath是否简单,因为我不确定如何使用UnsafePointer。

欢迎解释和示例。

您可以使用 CGPath(rect: CGRect, transform: UnsafePointer<CGAffineTransform>?) 并在 CGAffineTransform 中传递 nil 作为可选

let dirX = -spread
let rect = bounds.insetBy(dx: dirX, dy: dirX)
let cgPath = CGPath(rect: rect, transform: nil)

但这不会对您的应用程序运行产生任何影响,即使代码量也如此,因为无论如何都是一行