For anyone encountering the same problem, you may refer to the link: https://stackoverflow.com/questions/61566929/swiftui-multitouch-gesture-multiple-gestures
Post
Replies
Boosts
Views
Activity
@Claude31 thank you for your comment. I apologize if my question is unclear. My objective is to create a gesture event that generates as many draggable circles as there are fingers touching the screen. Each finger is able to move its corresponding circle position and change circle status(show or hide) w/o affecting other circles.
I edited my code using .simultaneousGesture modifier as shown below but ran into couple issues:
Performing a drag gesture with one finger triggers both drag states to be 'active', which is not what I want. Each finger should only trigger its corresponding drag state to be 'active'.
There is no circle or only one circle generated when performing a drag gesture with two or more fingers. The number of circles generated should be the same as the number of fingers used to perform the drag gesture.
When a finger is currently performing a drag gesture, performing a new drag gesture with another finger will cause the initial drag state to be "Inactive". I want the drag gesture to be considered ended only when the finger is lifted off the screen.
@GestureState private var state1: DragState = .inactive
@GestureState private var state2: DragState = .inactive
var body: some View {
ZStack {
Color.clear.contentShape(Rectangle())
VStack {
Text(state1.isActive.description)
Text(state2.isActive.description)
}
if state1.isActive {
Circle()
.frame(width: 100, height: 100)
.position(state1.location)
}
if state2.isActive {
Circle()
.frame(width: 100, height: 100)
.position(state2.location)
}
}
.foregroundStyle(.blue)
.gesture(dragGestures)
}
private var dragGestures: some Gesture {
dragGesture1.simultaneously(with: dragGesture2)
}
private var dragGesture1: some Gesture {
DragGesture(minimumDistance: 0)
.updating($state1) { value, state, _ in
state = value.location == .zero ? .inactive : .active(position: value.location)
}
}
private var dragGesture2: some Gesture {
DragGesture(minimumDistance: 0)
.updating($state2) { value, state, _ in
state = value.location == .zero ? .inactive : .active(position: value.location)
}
}