Introduction
SwiftUI gives us a specific property wrapper for tracking the state of the gesture. When you add multiple gestures to your app's view hierarchy, you need to decide how the gestures interact with each other. You use gesture composition to define the order SwiftUI recognizes gestures.
There are three gesture composition types:
- Simultaneous - A gesture that's a sequence of two gestures.
- Sequenced - A gesture containing two gestures that can happen at the same time with neither of them preceding the other.
- Exclusive - A gesture that consists of two gestures where only one of them can succeed.
When you combine gesture modifiers simultaneously, SwiftUI must recognize all subgesture patterns at the same time for it to recognize the combining gesture. When you sequence gesture modifiers one after the other, SwiftUI must recognize each subgesture in order. Finally, when you combine gestures exclusively, SwiftUI recognizes the entire gesture pattern when SwiftUI only recognizes one subgesture but not the others.
Sequence One Gesture After Another
When you sequence one gesture after another, SwiftUI recognizes the first gesture before it recognizes the second. For example, to require a long press before the user can drag a view, you sequence a DragGesture after a LongPressGesture.
Model Sequenced Gesture States
To make it easier to track complicated states, use an enumeration that captures all of the states you need to configure your views. In the following example, there are three important states,
- No interaction(inactive)
- long press in progress(pressing)
- Dragging(Dragging)
- struct DraggableCircle: View {
-
- enum DragState {
- case inactive
- case pressing
- case dragging(translation: CGSize)
-
- var translation: CGSize {
- switch self {
- case .inactive, .pressing:
- return .zero
- case .dragging(let translation):
- return translation
- }
- }
-
- var isActive: Bool {
- switch self {
- case .inactive:
- return false
- case .pressing, .dragging:
- return true
- }
- }
-
- var isDragging: Bool {
- switch self {
- case .inactive, .pressing:
- return false
- case .dragging:
- return true
- }
- }
- }
-
- @GestureState var dragState = DragState.inactive
- @State var viewState = CGSize.zero
Create Gesture and Update UI State
When you sequence two gestures, the callbacks capture the state of both gestures. In the update callback, the new value uses an enumeration to represent the combination of the possible gesture states. The code below converts the underlying gesture states into the high-level DragState enumeration defined above.
- var body: some View {
- let minimumLongPressDuration = 0.5
- let longPressDrag = LongPressGesture(minimumDuration: minimumLongPressDuration)
- .sequenced(before: DragGesture())
- .updating($dragState) { value, state, transaction in
- switch value {
-
- case .first(true):
- state = .pressing
-
- case .second(true, let drag):
- state = .dragging(translation: drag?.translation ?? .zero)
-
- default:
- state = .inactive
- }
- }
- .onEnded { value in
- guard case .second(true, let drag?) = value else { return }
- self.viewState.width += drag.translation.width
- self.viewState.height += drag.translation.height
- }
When the user begins pressing the view, the drag state changes to pressing and a shadow animates under the shape. After the user finishes the long press and the drag state changes to dragging, a border appears around the shape to indicate that the user may begin moving the view.
- return Circle()
- .fill(Color.blue)
- .overlay(dragState.isDragging ? Circle().stroke(Color.white, lineWidth: 2) : nil)
- .frame(width: 100, height: 100, alignment: .center)
- .offset(
- x: viewState.width + dragState.translation.width,
- y: viewState.height + dragState.translation.height
- )
- .animation(nil)
- .shadow(radius: dragState.isActive ? 8 : 0)
- .animation(.linear(duration: minimumLongPressDuration))
- .gesture(longPressDrag)
- }
Remember, one of the advantages of @GestureState is that it automatically sets the value of your property back to its initial value when the gesture ends. It means we can drag a view around all we want, and as soon as we let go it will snap back to its original position.