Track the distance between two fingers and use that information to scale or zoom your content.
A pinch gesture is a continuous gesture that tracks the distance between the first the first two fingers that touch the screen. Use the
UIPinch class to detect pinch 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.
A pinch gesture recognizer reports changes to the distance between two fingers touching the screen. Pinch gestures are continuous, so your action method is called each time the distance between the fingers changes. The distance between the fingers is reported as a scale factor. At the beginning of the gesture, the scale factor is
1. As the distance between the two fingers increases, the scale factor increases proportionally. Similarly, the scale factor decreases as the distance between the fingers decreases. Pinch gestures are used most commonly to change the size of objects or content onscreen. For example, map views use pinch gestures to change the zoom level of the map.
A pinch gesture recognizer enters the
UIGesture state only after the distance between the two fingers changes for the first time. After that initial change, subsequent changes to the distance put the gesture recognizer into the
UIGesture state and update the scale factor. When the user’s fingers lift from the screen, the gesture recognizer enters the
Listing 1 demonstrates how to resize a view linearly using a pinch gesture recognizer. This action method applies the current scale factor to the view’s transform and then resets the gesture recognizer’s
scale property to
1. Resetting the scale factor causes the gesture recognizer to report only the amount of change since the value was reset, which results in linear scaling of the view.
If the code for your pinch gesture recognizer is not called, or is not working correctly, 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
At least two fingers are touching the screen.
You are applying scale factors to your content correctly. Exponential growth of a value happens when you simply apply the scale factor to the current value.