官方对 coordinateSpace 的解释是:
Assigns a name to the view’s coordinate space, so other code can operate on dimensions like points and sizes relative to the named space.
我的理解是,用户可以给某个 view 的空间坐标取个名字,然后获取到其他 view 在指定名字空间中的相对坐标。
比如横向排列 3 个圆形。
⭕️⭕️⭕️
计算第 2 个圆形相对于第 3 个圆形的相对坐标。由于第 2 个⭕️在第三个⭕️的左侧,相对坐标的 x 值,应该是负数。
在第 2 个⭕️中,print(proxy.frame(in: .named("circle3")).origin)的输出结果总是正的。
是我对 coordinateSpace 的理解有问题吗?
var body: some View {
HStack(spacing:0) {
Circle()
.fill(.red)
.frame(width: 100, height: 100)
.coordinateSpace(name: "circle1")
GeometryReader{ proxy in
Circle()
.fill(.green)
.frame(width: 100, height: 100)
.coordinateSpace(name: "circle2")
.onTapGesture {
print(proxy.frame(in: .named("circle3")).origin);
}
}
Circle()
.fill(.blue)
.frame(width: 100, height: 100)
.coordinateSpace(name: "circle3")
}
.coordinateSpace(name: "stack")
}
看官方说明完全找不到头绪。 https://developer.apple.com/documentation/swiftui/view/coordinatespace(name:)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.