如何解决SwiftUI将ForEach循环的元素附加到全局位置
我想将元素附加到(全局!)位置。听起来很简单,但元素(此处为Circle)在ScrollView和ForEach循环中声明。重要的是,Circle的声明必须保持不变(示例已简化)。我的目标是当用户滚动列表时元素不会移动。
GIF:这些圆圈应该位于彼此的顶部,并且它们不应移动。
struct ContentView: View {
@State var elements = ["1","2","3"]
var body: some View {
VStack{
ScrollView{
ForEach(elements,id: \.self){ elem in
ZStack{
Rectangle()
.foregroundColor(Color.gray.opacity(0.2))
.frame(width: 300,height: 50)
Text("\(elem)")
}
// the circle below should stay in the ForEach loop!
.overlay(Circle() // <- I tried to use an overlay,but that does not work
.stroke(lineWidth: 5)
.foregroundColor(.red)
.frame(width: 50,height: 50)
.position(x: 100,y: 100) // <- can't set global position!
)
.padding(.top,20)
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。