Handling Long-Press Gesture

Doyeon
doyeona
Published in
3 min readJan 13, 2021

Detect extended duration taps on the screen, and use them to reveal contextually relevant content.

Long press (aka press-and-hold) gestures detect one or more fingers touching the screen for an extended period of time. You can set for the minimum duration of touching the screen.

How to implement the gesture recognizer?

  • Programmatically using addGestureRecognizer(_:) method.
  • In Interface Builder.

Long-press gestures are continuous gestures, which means whenever the state changes, the action method will be called.

  • UIGesture.Recognizer.State.began
  • UIGesture Recognizer.State.changed
  • UIGesture Recognizer.State.ended

UIGesture.Recognizer.State.began

when you touch the screen at least minimum press duration (default : 5 sec) with numberOfTouchesRequired within the area(allowableMovement) that is allowed, then the state will be set to began.

UIGesture.Recognizer.State.changed

but while touching the screen, when you move your finger, the state will change.

UIGesture.Recognizer.State.ended

Lastly when you are no longer touching the screen, then the state will be ended.

Long Press Gesture Recognizer Properties

  • Min Duration : minimum press duration default value is 0.5
  • Recognize Taps : How many taps you need to touch the screen. The default is 0 which means one touch is required. if it’s 1, you have to tap the screen twice.
  • Recognize Touches : How many required fingers need to touch the screen. if it’s 2, then you should use two fingers to touch the screen.
  • Tolerance: allowableMovement, setting up the maximum area that is allowed for movement. For example, when you set the value to 1000, then the recognizer will recognize your wider area of touches but when your touch is out of range, then the gesture will fail.

How to implement it programmatically?

For me, I wanted each cell in CollectionView to recognize long press gestures and the ways to implement it to the view and other layouts will be similar to this.

private func setupLongGestureRecognizerOnCollection() {
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
self.collectionView.addGestureRecognizer(lpgr)
}

So when the gesture is active, it will call an action method, handleLongPress. Below code is simply checking the state of the gestureRecognizer whether it began or not and gestureRecognizer.location will return the point computed as the location to get the pressed index path. then print the index.

@objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {     guard gestureReconizer.state != .began else { return }
let point = gestureReconizer.location(in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: point)
if let index = indexPath{
print(index.row)
}
else{
print("Could not find index path")
}
}

After i implemented Long gesture recognizer, I decided to use Context Menus 🤦🏻‍♀️ which is kinda cool like 3D touch in iOS however it’s only available at least iOS 13.

Context Menus

click the link below to read about context menu ✊🏻

references :

--

--

Doyeon
doyeona

Passionate iOS developer creating extraordinary apps. Innovative, elegant, and collaborative. Constantly pushing boundaries.Let's build the future together📱✨🚀