Article

Handling Long-Press Gestures

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

Overview

Long-press (also known as press-and-hold) gestures detect one or more fingers (or a stylus) touching the screen for an extended period of time. You configure the minimum duration required to recognize the press and the number of times the fingers must be touching the screen. (The gesture recognizer is triggered only by the duration of the touches and not by the force associated with them.) You might use a long-press gesture to initiate an action on the object being pressed. For example, you might use it to display a context-sensitive menu.

You can attach a gesture recognizer in one of these ways:

  • Programmatically. Call the addGestureRecognizer(_:) method of your view.

  • In Interface Builder. Drag the appropriate object from the library and drop it onto your view.

A diagram showing a single-finger long-press gesture

Long-press gestures are continuous gestures, meaning that your action method may be called multiple times as the state changes. After the user's fingers have touched the screen for the minimum amount of time time, a long-press gesture recognizer enters the UIGestureRecognizer.State.began state. The gesture recognizer moves to the UIGestureRecognizer.State.changed state if the fingers move or if any other changes occur to the touches. The gesture recognizer remains in the UIGestureRecognizer.State.changed state as long as the fingers remain down, even if those fingers move outside of the initial view. When the user’s fingers lift from the screen, the gesture recognizer enters the UIGestureRecognizer.State.ended state.

Listing 1 shows an action method that displays a context menu on top of the view. It displays the context menu at the beginning of the gesture, while the user’s finger is still on the screen. The view controller that implements this method also sets itself as the first responder so that it can respond to menu actions selected by the user.

Listing 1

Handling a long-press gesture

@IBAction func showResetMenu(_ gestureRecognizer: UILongPressGestureRecognizer) {
   if gestureRecognizer.state == .began {
      self.becomeFirstResponder()
      self.viewForReset = gestureRecognizer.view

      // Configure the menu item to display
      let menuItemTitle = NSLocalizedString("Reset", comment: "Reset menu item title")
      let action = #selector(ViewController.resetPiece(controller:))
      let resetMenuItem = UIMenuItem(title: menuItemTitle, action: action)

      // Configure the shared menu controller
      let menuController = UIMenuController.shared
      menuController.menuItems = [resetMenuItem]

      // Set the location of the menu in the view.
      let location = gestureRecognizer.location(in: gestureRecognizer.view)
      let menuLocation = CGRect(x: location.x, y: location.y, width: 0, height: 0)
      menuController.setTargetRect(menuLocation, in: gestureRecognizer.view!)

      // Show the menu.
      menuController.setMenuVisible(true, animated: true)
   }
}

If the code for your long-press gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:

See Also

Gestures

Handling Tap Gestures

Use brief taps on the screen to implement button-like interactions with your content.

Handling Pan Gestures

Trace the movement of fingers around the screen, and apply that movement to your content.

Handling Swipe Gestures

Detect a horizontal or vertical swipe motion on the screen, and use it to trigger navigation through your content.

Handling Pinch Gestures

Track the distance between two fingers and use that information to scale or zoom your content.

Handling Rotation Gestures

Measure the relative rotation of two fingers on the screen, and use that motion to rotate your content.