首页 > 解决方案 > SwiftUI 中的 CGPath

问题描述

在使用SwiftUI时,我对这行代码感到困惑:

xPath = Path(myPath.cgPath)

这里有一个更广泛的背景:

struct MyShape: Shape {
    @Binding var xPath:Path
    ........


    var myPath = Path()
    myPath.move(to: somePoint1)
    myPath.addLine(to: somePoint2)
    myPath.addLine(to: somePoint3)
    myPath.addLine(to: somePoint1)

    xPath = Path(myPath.cgPath)

在执行该行之后,这是我在调试器控制台中得到的:

(lldb) p myPath
(Path) $R14 = {
  storage = path (path = 0x0000000280e081c0)
}
(lldb) p myPath.cgPath
(NSObject) $R16 = 0x000000028298cfc0 {
  baseNSObject@0 = {
    isa = __NSCFType
  }
}
(lldb) p myPath.description
(String) $R18 = "39.3916 34.8342 m 51.5 0 l 63.6084 34.8342 l 39.3916 34.8342 l"
(lldb) p xPath
(Path) $R20 = {
  storage = empty
}

这看起来好吗?由于 myPath 包含某些内容,我觉得奇怪的是 xPath 看起来最后是空的。

我错过了什么吗?

标签: iosswiftswiftuicgpath

解决方案


推荐阅读