How do I enable zooming in a UIScrollView
?
Answer is here:
A scroll view also handles zooming and panning of content. As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should update subviews of the content as necessary. (Note that the gesture can end and a finger could still be down.) While the gesture is in progress, the scroll view does not send any tracking calls to the subview.
The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum (minimumZoomScale) zoom scale must be different.
So:
UIScrollViewDelegate
and is set to delegate
on your UIScrollView
instanceviewForZoomingInScrollView:
(which must return the content view you're interested in zooming). You can also implement scrollViewDidEndZooming:withView:atScale:
optionally.UIScrollView
instance, you have to set the minimumZoomScale
and the maximumZoomScale
to be different (they are 1.0 by default).Note: The interesting thing about this is what if you want to break zooming. Is it enough to return nil
in the viewForZooming...
method? It does break zooming, but some of the gestures will be messed up (for two fingers). Therefore, to break zooming you should set the min and max zoom scale to 1.0.