Trace the movement of fingers around the screen, and apply that movement to your content.
A pan gesture occurs any time the user moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the
UIPan class for pan gestures and the
UIScreen class for screen-edge pan gestures.
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.
Use pan gesture recognizers for tasks that require you to track the movement of the user’s fingers onscreen. You might use a pan gesture recognizer to drag objects around in your interface or update their appearance based on the position of the user’s finger. Pan gestures are continuous, so your action method is called whenever the touch information changes, giving you a chance to update your content.
A pan gesture recognizer enters the
UIGesture state as soon as the required amount of initial movement is achieved. After that initial change, subsequent changes cause the gesture recognizer to enter the
UIGesture state. When the user’s fingers lift from the screen, the gesture recognizer enters the
To simplify tracking, use the pan gesture recognizer’s
translation(in:) method to get the distance that the user’s finger has moved from the original touch location. At the beginning of the gesture, a pan gesture recognizer stores the initial point of contact for the user’s fingers. (If the gesture involves multiple fingers, the gesture recognizer uses the center point of the set of touches.) Each time the fingers move, the
translation(in:) method reports the distance from the original location.
Listing 1 shows an action method used to drag a view around the screen. When the gesture begins, this method saves the initial position of the view. It then updates the position of the view based on the movement of the user's fingers.
If the code for your pan 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