所以我一直在 Xcode 中玩耍。我有一个问题。问题是,当我使用 dragGesture 并将多个项目(在本例中为矩形)放在屏幕上时,当我只想移动一个项目时,它们都会移动。
我想单独移动所有项目。我怎么做?
这是我写的代码:
import SwiftUI
struct GameView: View {
@State var offset: CGSize = .zero
var body: some View {
ZStack{
Image("Wolffront")
VStack(spacing: 10){
Rectangle()
.fill(.green)
.frame(width: 50, height: 50)
.cornerRadius(10)
.shadow(color: .black, radius: 10, x: 10, y: 10)
.navigationBarHidden(true)
.overlay(
Text("O")
.font(.largeTitle)
.foregroundColor(.white)
)
.offset(offset)
.gesture(DragGesture()
.onChanged { value in
withAnimation(.spring()) {
offset = value.translation
}
}
.onEnded {value in
withAnimation(.spring()) {
offset = value .translation
}
}
)
Rectangle()
.fill(.green)
.frame(width: 50, height: 50)
.cornerRadius(10)
.shadow(color: .black, radius: 10, x: 10, y: 10)
.navigationBarHidden(true)
.overlay(
Text("O")
.font(.largeTitle)
.foregroundColor(.white)
)
.offset(offset)
.gesture(DragGesture()
.onChanged { value in
withAnimation(.spring()) {
offset = value.translation
}
}
.onEnded {value in
withAnimation(.spring()) {
offset = value .translation
}
}
)
}
}
}
}
struct GameView_Previews: PreviewProvider {
static var previews: some View {
GameView()
}
}