Ios ';原产地';在SwiftUI中创建视图
我在玩Ios ';原产地';在SwiftUI中创建视图,ios,swift,layout,swiftui,frame,Ios,Swift,Layout,Swiftui,Frame,我在玩.position(x:y:)和CoordinateSpace.global时遇到了一些让我困惑的东西 我试图确定ZStack原点在全局坐标空间中的坐标。然而,当我在这些坐标上放置一个点时,它并没有与ZStack的左上角对齐 这就是我使用的代码: struct Test: View { var body: some View { ZStack { Image(systemName: "circle.fill")
.position(x:y:)
和CoordinateSpace.global
时遇到了一些让我困惑的东西
我试图确定ZStack原点在全局坐标空间中的坐标。然而,当我在这些坐标上放置一个点时,它并没有与ZStack的左上角对齐
这就是我使用的代码:
struct Test: View {
var body: some View {
ZStack {
Image(systemName: "circle.fill")
.resizable()
.frame(width: 5, height: 5)
.foregroundColor(.red)
.position(x: 177, y: 423) // This is from 'frame.origin.x' and 'frame.origin.y'
ZStack { // Trying to determine this ZStack's coordinates in the global coordinate space
GeometryReader { geometry -> AnyView in // Used to retrieve the coordinates using `geometry` and then returning a Text so I can see the coordinates on screen
let frame = geometry.frame(in: CoordinateSpace.global)
return AnyView(Text("\(frame.origin.x), \(frame.origin.y)").fixedSize(horizontal: false, vertical: true))
}
}
.frame(width: 60, height: 60)
}
}
}
这就是圆点出现的地方:有人知道为什么它会出现在那个奇怪的地方,而它本应该出现在ZStack的左上角吗?我以为原点应该在视图的左上角?您通过几何读取器在全局坐标空间中计算帧,但图像被放置在外部ZStack坐标空间中,这就是错位的原因 为了使其按预期工作(接受用于测试目的的硬编码坐标),坐标应在一个坐标空间中生成 这里有一个解决方案
var主体:一些视图{
ZStack{
图像(系统名称:“圆形填充”)
.可调整大小()
.框架(宽度:5,高度:5)
.foregroundColor(.red)
.position(x:177,y:379)//这是来自“frame.origin.x”和“frame.origin.y”
ZStack{
GeometryReader{geometry->中的任意视图
让frame=geometry.frame(in:.named(“test”)//不清楚您试图实现什么?您能详细说明一下吗?哪个视图以及要放置在哪里。@Asperi这样更好吗?我试图确定ZStack的原点
var body: some View {
ZStack {
Image(systemName: "circle.fill")
.resizable()
.frame(width: 5, height: 5)
.foregroundColor(.red)
.position(x: 177, y: 379) // This is from 'frame.origin.x' and 'frame.origin.y'
ZStack {
GeometryReader { geometry -> AnyView in
let frame = geometry.frame(in: .named("test")) // << here !!
return AnyView(Text("\(frame.origin.x), \(frame.origin.y)")
.fixedSize(horizontal: false, vertical: true))
}
}
.frame(width: 60, height: 60)
}.coordinateSpace(name: "test") // << here !!
}