SwiftUI_visionOS_Note

.onLongPressGesture

May 27, 2024
LongPressGesture | Apple Developer Documentation
A gesture that succeeds when the user performs a long press.
icon

https://developer.apple.com/documentation/swiftui/longpressgesture

Handling long-press gestures | Apple Developer Documentation
Detect extended duration taps on the screen, and use them to reveal contextually relevant content.
icon

https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_long-press_gestures

 

1. Base structure

 
 
  • onLongPressGesture: This is the core interaction:
    • minimumDuration: 3.0 means the press needs to last at least 3 seconds.
    • maximumDistance: 50 means the finger can't move more than 50 points during the press.
    • Two blocks of code:
      • Start of Press: The isPressing block triggers when the gesture starts and meets the minimum duration:
        • The isComplete variable is toggled (flipped between true and false) using an animation to make the rectangle grow smoothly.
      • End of Press: The second block triggers when the gesture ends. If the action hasn't succeeded (!isSuccess) it resets the isComplete variable after a slight delay (DispatchQueue.main.asyncAfter).
    • Gesture Completion (perform:): If the gesture meets all requirements, this block runs and sets isSuccess to true,also with an animation.

You might also like

BlogPro logo
Made with BlogPro