Detect a horizontal or vertical swipe motion on the screen, and use it to trigger navigation through your content.
A swipe gesture occurs when the user moves one or more fingers across the screen in a specific horizontal or vertical direction. Use the
UISwipe class to detect swipe 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.
UISwipe object tracks the motion of the user’s finger across the screen either horizontally or vertically. A swipe requires the user’s finger to move in a specific direction and not deviate significantly from the main direction of travel. (The direction and number of fingers required for the gesture are configurable.) Swipe gestures are discrete, so your action method is called only after the gesture ends successfully. As a result, swipes are most appropriate when you care only about the results of the gesture and not about tracking the movement of the user’s finger.
Listing 1 shows a skeletal action method for a swipe gesture recognizer. You would use a method like this to perform a task when the gesture is recognized. Because the gesture is discrete, the gesture recognizer does not enter the began or changed states.
If the code for your swipe gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed: