import FoldFormat import SwiftUI extension View { /// Lets a one-finger drag anywhere on the view drive the session's fold step: the moving faces /// follow the finger along their screen path, and the step completes when the finger lifts at /// t = 1. Releasing below 1 leaves the paper where it is. func foldGesture(_ session: FoldSession) -> some View { modifier(StepDragGesture(session: session, kinds: [.fold, .foldAndUnfold], begin: session.beginFoldDrag)) } } /// A one-finger drag that drives the session's step while it is one of `kinds`. struct StepDragGesture: ViewModifier { let session: FoldSession let kinds: Set let begin: () -> Void @GestureState private var isDragging = false func body(content: Content) -> some View { content .gesture(drag, isEnabled: kinds.contains(session.step.kind) && !session.isLocked) .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } } /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). private var drag: some Gesture { DragGesture() .updating($isDragging) { value, isDragging, _ in if !isDragging { begin() } isDragging = true session.drag(translation: SIMD2(value.translation.width, value.translation.height)) } .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) } } }