iOS UIGestureRecognizer UILongPressGestureRecognizer

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The UILongPressGestureRecognizer lets you listen for a long press on a view. You can set the length of delay before the action method is called.

Swift

override func viewDidLoad() {
    super.viewDidLoad()

    // Long Press
    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
    longPressView.addGestureRecognizer(longPressGesture)
}

// Long press action
func handleLongPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.Began {
        label.text = "Long press recognized"
    }
}

Notes

  • A fuller sample project can be found here.

  • Change the minimumPressDuration to set the length of long press.



Got any iOS Question?