Detect extended duration taps on the screen, and use them to reveal contextually relevant content.
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
addmethod of your view.
In Interface Builder. Drag the appropriate object from the library and drop it onto your view.
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
UIGesture state. The gesture recognizer moves to the
UIGesture state if the fingers move or if any other changes occur to the touches. The gesture recognizer remains in the
UIGesture 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
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.
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:
isproperty of the view is set to
User Interaction Enabled
true. Image views and labels set this property to
The tap duration was greater than what is specified in the
The number of taps was equal to the number specified in the
Of Taps Required
The number of fingers was equal to the number specified in the
Of Touches Required