Hi, I have a code snippet as below that creates a draggable circle when a drag gesture is performed, and the circle disappears when the drag gesture is released. I'm wondering how I can make it detect multiple drag gestures. More specifically, when a finger is performing drag gesture, if another finger presses the screen at this time, a new circle will be generated.
struct ContentView: View {
enum DragState {
case inactive
case active(position: CGPoint)
var location: CGPoint {
switch self {
case .inactive:
return .zero
case .active(let position):
return position
}
}
var isActive: Bool {
switch self {
case .inactive:
return false
case .active:
return true
}
}
}
@GestureState private var state: DragState = .inactive
var body: some View {
ZStack {
Color.clear.contentShape(Rectangle())
if state.isActive {
Circle()
.frame(width: 100, height: 100)
.foregroundStyle(.blue)
.position(state.location)
}
}
.gesture(dragGesture)
}
private var dragGesture: some Gesture {
DragGesture(minimumDistance: 0)
.updating($state) { value, state, _ in
state = value.location == .zero ? .inactive : .active(position: value.location)
}
}
}
For anyone encountering the same problem, you may refer to the link: https://stackoverflow.com/questions/61566929/swiftui-multitouch-gesture-multiple-gestures