PluginBench
Skill
Pass
Audit score 90

swiftui-gestures

dpearson2699/swift-ios-skills

Implement and compose SwiftUI gestures with modern APIs, state management, and conflict resolution.

What is swiftui-gestures?

This skill covers SwiftUI gesture recognition, composition, and state management using Swift 6.3 patterns. Use it when adding tap, long press, drag, magnify, or rotate gestures; composing gestures with simultaneously/sequenced/exclusively; managing transient state with @GestureState; resolving parent/child gesture conflicts; or migrating to newer gesture APIs like MagnifyGesture and RotateGesture.

  • Implement discrete and continuous gestures (TapGesture, LongPressGesture, DragGesture, MagnifyGesture, RotateGesture, SpatialTapGesture)
  • Compose gestures using simultaneously(), sequenced(), and exclusively() for complex interactions
  • Manage transient gesture state with @GestureState and persist values with @State
  • Resolve gesture conflicts with highPriorityGesture and simultaneousGesture modifiers
  • Build custom Gesture protocol conformances
  • Migrate from deprecated MagnificationGesture and RotationGesture to iOS 17+ alternatives

How to install swiftui-gestures

npx skills add https://github.com/dpearson2699/swift-ios-skills --skill swiftui-gestures
Claude Code
Cursor
Windsurf
Cline

How to use swiftui-gestures

  1. 1.Identify which gesture type matches your interaction (tap, long press, drag, magnify, rotate)
  2. 2.Choose between @GestureState for transient feedback and @State for persistent values
  3. 3.Apply the gesture using .gesture() modifier or shorthand methods like .onTapGesture()
  4. 4.For multiple gestures, compose them with simultaneously(), sequenced(), or exclusively()
  5. 5.Test gesture priority and conflicts; use highPriorityGesture or simultaneousGesture if needed

Use cases

Good for
  • Adding multi-tap, long-press, and drag interactions to custom views
  • Implementing pinch-to-zoom and two-finger rotation on images
  • Composing long-press-then-drag sequences for reordering or moving items
  • Handling simultaneous magnify and rotate gestures on photos
  • Resolving gesture priority conflicts between parent and child views
Who it's for
  • iOS app developers building custom gesture interactions
  • Engineers migrating legacy gesture code to modern SwiftUI APIs
  • Developers implementing complex multi-gesture compositions
  • Anyone handling gesture state management and accessibility alternatives

swiftui-gestures FAQ

What's the difference between @GestureState and @State?

@GestureState automatically resets to its initial value when the gesture ends, making it ideal for transient visual feedback. @State persists across gesture cycles, so use it for values that should remain after the gesture completes.

When should I use simultaneously() vs sequenced()?

Use simultaneously() when both gestures should be recognized at the same time (e.g., pinch and rotate together). Use sequenced() when the first gesture must succeed before the second begins (e.g., long press then drag).

What's the difference between MagnifyGesture and the deprecated MagnificationGesture?

MagnifyGesture is the iOS 17+ replacement for MagnificationGesture. It provides the same pinch-to-zoom functionality but with updated API design. Use MagnifyGesture for new code.

How do I handle gesture conflicts between parent and child views?

Use highPriorityGesture() to give a gesture priority over others, or simultaneousGesture() to allow multiple gestures to recognize at once. This resolves conflicts where parent and child both want to handle the same gesture.

Can I use DragGesture.Value.velocity?

Yes, DragGesture.Value includes velocity information (available since iOS 13+). Use it to implement momentum-based animations or predict where a drag will end.

Full instructions (SKILL.md)

Source of truth, from dpearson2699/swift-ios-skills.


name: swiftui-gestures description: "Implement, review, or improve SwiftUI gesture handling. Use when adding tap, long press, drag, magnify, or rotate gestures, composing gestures with simultaneously/sequenced/exclusively, managing transient state with @GestureState, resolving parent/child gesture conflicts with highPriorityGesture or simultaneousGesture, building custom Gesture protocol conformances, or migrating from deprecated MagnificationGesture to MagnifyGesture or using the newer RotateGesture."

SwiftUI Gestures (iOS 26+)

Review, write, and fix SwiftUI gesture interactions. Apply modern gesture APIs with correct composition, state management, and conflict resolution using Swift 6.3 patterns.

Scope boundary: This skill owns SwiftUI gesture recognition, composition, gesture state, and gesture-specific accessibility alternatives. Broader SwiftUI architecture/state ownership belongs in swiftui-patterns; list, scroll, form, and control layout belongs in swiftui-layout-components; broad UIKit bridging belongs in swiftui-uikit-interop.

When correcting Apple API availability, deprecation, or behavior claims, cite the relevant Sosumi or official Apple documentation URL in the response.

Contents

Gesture Overview

GestureTypeValueSince
TapGestureDiscreteVoidiOS 13
LongPressGestureDiscreteBooliOS 13
DragGestureContinuousDragGesture.ValueiOS 13
MagnifyGestureContinuousMagnifyGesture.ValueiOS 17
RotateGestureContinuousRotateGesture.ValueiOS 17
SpatialTapGestureDiscreteSpatialTapGesture.ValueiOS 16

Discrete gestures fire once (.onEnded). Continuous gestures stream updates (.onChanged, .onEnded, .updating).

TapGesture

Recognizes one or more taps. Use the count parameter for multi-tap.

// Single, double, and triple tap
TapGesture()            .onEnded { tapped.toggle() }
TapGesture(count: 2)    .onEnded { handleDoubleTap() }
TapGesture(count: 3)    .onEnded { handleTripleTap() }

// Shorthand modifier
Text("Tap me").onTapGesture(count: 2) { handleDoubleTap() }

LongPressGesture

Succeeds after the user holds for minimumDuration. Fails if finger moves beyond maximumDistance.

// Basic long press (0.5s default)
LongPressGesture()
    .onEnded { _ in showMenu = true }

// Custom duration and distance tolerance
LongPressGesture(minimumDuration: 1.0, maximumDistance: 10)
    .onEnded { _ in triggerHaptic() }

With visual feedback via @GestureState + .updating():

@GestureState private var isPressing = false

Circle()
    .fill(isPressing ? .red : .blue)
    .scaleEffect(isPressing ? 1.2 : 1.0)
    .gesture(
        LongPressGesture(minimumDuration: 0.8)
            .updating($isPressing) { current, state, _ in state = current }
            .onEnded { _ in completedLongPress = true }
    )

Shorthand: .onLongPressGesture(minimumDuration:perform:onPressingChanged:).

DragGesture

Tracks finger movement. Value provides startLocation, location, translation, velocity, and predictedEndTranslation. DragGesture.Value.velocity is available with DragGesture from iOS 13+; do not confuse it with iOS 17+ gesture types such as MagnifyGesture and RotateGesture.

@State private var offset = CGSize.zero

RoundedRectangle(cornerRadius: 16)
    .fill(.blue)
    .frame(width: 100, height: 100)
    .offset(offset)
    .gesture(
        DragGesture()
            .onChanged { value in offset = value.translation }
            .onEnded { _ in withAnimation(.spring) { offset = .zero } }
    )

Configure minimum distance and coordinate space:

DragGesture(minimumDistance: 20, coordinateSpace: .global)

MagnifyGesture (iOS 17+)

Replaces the deprecated MagnificationGesture. Tracks pinch-to-zoom scale.

@GestureState private var magnifyBy = 1.0

Image("photo")
    .resizable().scaledToFit()
    .scaleEffect(magnifyBy)
    .gesture(
        MagnifyGesture()
            .updating($magnifyBy) { value, state, _ in
                state = value.magnification
            }
    )

With persisted scale:

@State private var currentScale = 1.0
@GestureState private var gestureScale = 1.0

Image("photo")
    .scaleEffect(currentScale * gestureScale)
    .gesture(
        MagnifyGesture(minimumScaleDelta: 0.01)
            .updating($gestureScale) { value, state, _ in state = value.magnification }
            .onEnded { value in
                currentScale = min(max(currentScale * value.magnification, 0.5), 5.0)
            }
    )

RotateGesture (iOS 17+)

RotateGesture is the newer alternative to RotationGesture. Tracks two-finger rotation angle.

@State private var angle = Angle.zero

Rectangle()
    .fill(.blue).frame(width: 200, height: 200)
    .rotationEffect(angle)
    .gesture(
        RotateGesture(minimumAngleDelta: .degrees(1))
            .onChanged { value in angle = value.rotation }
    )

With persisted rotation:

@State private var currentAngle = Angle.zero
@GestureState private var gestureAngle = Angle.zero

Rectangle()
    .rotationEffect(currentAngle + gestureAngle)
    .gesture(
        RotateGesture()
            .updating($gestureAngle) { value, state, _ in state = value.rotation }
            .onEnded { value in currentAngle += value.rotation }
    )

Gesture Composition

.simultaneously(with:) — both gestures recognized at the same time

let magnify = MagnifyGesture()
    .onChanged { value in scale = value.magnification }

let rotate = RotateGesture()
    .onChanged { value in angle = value.rotation }

Image("photo")
    .scaleEffect(scale)
    .rotationEffect(angle)
    .gesture(magnify.simultaneously(with: rotate))

The value is SimultaneousGesture.Value with .first and .second optionals.

.sequenced(before:) — first must succeed before second begins

let longPressBeforeDrag = LongPressGesture(minimumDuration: 0.5)
    .sequenced(before: DragGesture())
    .onEnded { value in
        guard case .second(true, let drag?) = value else { return }
        finalOffset.width += drag.translation.width
        finalOffset.height += drag.translation.height
    }

.exclusively(before:) — only one succeeds (first has priority)

let doubleTapOrLongPress = TapGesture(count: 2)
    .exclusively(before:
        LongPressGesture()
    )
    .onEnded { result in
        switch result {
        case .first(_): handleDoubleTap()
        case .second(_): handleLongPress()
        }
    }

@GestureState

@GestureState is a property wrapper that automatically resets to its initial value when the gesture ends. Use for transient feedback; use @State for values that persist.

@GestureState private var dragOffset = CGSize.zero  // resets to .zero
@State private var position = CGSize.zero            // persists

Circle()
    .offset(
        x: position.width + dragOffset.width,
        y: position.height + dragOffset.height
    )
    .gesture(
        DragGesture()
            .updating($dragOffset) { value, state, _ in
                state = value.translation
            }
            .onEnded { value in
                position.width += value.translation.width
                position.height += value.translation.height
            }
    )

Custom reset with animation: @GestureState(resetTransaction: Transaction(animation: .spring))

Adding Gestures to Views

Three modifiers control gesture priority in the view hierarchy:

ModifierBehavior
.gesture()Lower precedence than gestures already defined by the view or its children.
.highPriorityGesture()Added gesture takes precedence over existing gestures.
.simultaneousGesture()Added gesture processes at the same priority as existing gestures.
// Default: the child tap wins on the image; the parent handles empty stack space.
VStack {
    Image(systemName: "star.fill")
        .onTapGesture { handleChild() }

    Rectangle().fill(.blue)
}
.gesture(TapGesture().onEnded { handleParent() })

// Use simultaneousGesture when both handlers should run on child content.
VStack {
    Image(systemName: "star.fill")
        .onTapGesture { handleChild() }
}
.simultaneousGesture(TapGesture().onEnded { handleParent() })

// Use highPriorityGesture only when the parent should win.
VStack {
    Text("Child")
        .gesture(TapGesture().onEnded { handleChild() })
}
.highPriorityGesture(TapGesture().onEnded { handleParent() })

GestureMask

Control which gestures participate when using .gesture(_:including:):

.gesture(drag, including: .gesture)   // added gesture; disables subview gestures
.gesture(drag, including: .subviews)  // subview gestures; disables added gesture
.gesture(drag, including: .all)       // default: added + subview gestures
.gesture(drag, including: .none)      // disables added + subview gestures

Custom Gesture Protocol

Create reusable gestures by conforming to Gesture:

struct SwipeGesture: Gesture {
    enum Direction { case left, right, up, down }
    typealias Value = Direction

    let minimumDistance: CGFloat

    init(minimumDistance: CGFloat = 50) {
        self.minimumDistance = minimumDistance
    }

    var body: AnyGesture<Direction> {
        AnyGesture(
            DragGesture(minimumDistance: minimumDistance)
                .map { value in
                    let h = value.translation.width, v = value.translation.height
                    if abs(h) > abs(v) {
                        return h > 0 ? .right : .left
                    } else {
                        return v > 0 ? .down : .up
                    }
                }
        )
    }
}

// Usage
Rectangle().gesture(SwipeGesture().onEnded { print("Swiped \($0)") })

Wrap in a View extension for ergonomic API:

extension View {
    func onSwipe(perform action: @escaping (SwipeGesture.Direction) -> Void) -> some View {
        gesture(SwipeGesture().onEnded(action))
    }
}

Common Mistakes

1. Misreading parent/child gesture precedence

// DON'T: Assume parent .gesture() overrides the child tap
VStack {
    Image(systemName: "star.fill")
        .onTapGesture { childAction() }
}
.gesture(TapGesture().onEnded { parentAction() })

// DO: Pick the relationship explicitly
VStack {
    Image(systemName: "star.fill")
        .onTapGesture { childAction() }
}
.simultaneousGesture(TapGesture().onEnded { parentAction() })

// Or use .highPriorityGesture() when the parent should take precedence.

2. Using @State instead of @GestureState for transient state

// DON'T: @State doesn't auto-reset — view stays offset after gesture ends
@State private var dragOffset = CGSize.zero

DragGesture()
    .onChanged { value in dragOffset = value.translation }
    .onEnded { _ in dragOffset = .zero }  // manual reset required

// DO: @GestureState auto-resets when gesture ends
@GestureState private var dragOffset = CGSize.zero

DragGesture()
    .updating($dragOffset) { value, state, _ in
        state = value.translation
    }

3. Not using .updating() for intermediate feedback

// DON'T: No visual feedback during long press
LongPressGesture(minimumDuration: 2.0)
    .onEnded { _ in showResult = true }

// DO: Provide feedback while pressing
@GestureState private var isPressing = false

LongPressGesture(minimumDuration: 2.0)
    .updating($isPressing) { current, state, _ in
        state = current
    }
    .onEnded { _ in showResult = true }

4. Using deprecated gesture types on iOS 17+

// DON'T: Deprecated since iOS 17
MagnificationGesture()   // deprecated — use MagnifyGesture()

// DO: Use newer gesture types
MagnifyGesture()         // iOS 17+
RotateGesture()          // iOS 17+ (newer alternative to RotationGesture)

5. Heavy computation in onChanged

// DON'T: Expensive work called every frame (~60-120 Hz)
DragGesture()
    .onChanged { value in
        let result = performExpensiveHitTest(at: value.location)
        let filtered = applyComplexFilter(result)
        updateModel(filtered)
    }

// DO: Throttle or defer expensive work
DragGesture()
    .onChanged { value in
        dragPosition = value.location  // lightweight state update only
    }
    .onEnded { value in
        performExpensiveHitTest(at: value.location)  // once at end
    }

6. Using onTapGesture for actions that should be a Button

// DON'T: onTapGesture has no accessibility traits, VoiceOver role,
// Voice Control targeting, Switch Control scanning, or keyboard activation
Text("Delete")
    .onTapGesture { deleteItem() }

// DO: Button provides all of these automatically
Button("Delete", role: .destructive) { deleteItem() }

// DO: For custom visuals, use ButtonStyle instead of onTapGesture
Button { toggleExpanded() } label: {
    CardView()
}
.buttonStyle(.plain)

Reserve onTapGesture for multi-tap (count: 2+), tap-location-dependent behavior, or adding tap recognition to non-interactive content that already has appropriate accessibility traits.

Review Checklist

  • Correct gesture type: MagnifyGesture/RotateGesture (not deprecated Magnification/Rotation variants)
  • @GestureState used for transient values that should reset; @State for persisted values
  • .updating() provides intermediate visual feedback during continuous gestures
  • Parent/child conflicts resolved with .highPriorityGesture() or .simultaneousGesture()
  • onChanged closures are lightweight — no heavy computation every frame
  • Composed gestures use correct combinator: simultaneously, sequenced, or exclusively
  • Persisted scale/rotation clamped to reasonable bounds in onEnded
  • Custom Gesture conformances return a gesture body; use AnyGesture<Value> when mapping to a custom Value
  • Gesture-driven animations use .spring or similar for natural deceleration
  • GestureMask considered when mixing gestures across view hierarchy levels
  • onTapGesture only used where count > 1, tap location, or coordinate space matters — plain single-tap actions use Button instead

References