Zooming

In the simplest case, zooming is just a scaling transform that the scroll view applies to a subview. To implement zooming of a scroll view’s contents, you set the scroll view’s minimumZoomScale and maximumZoomScale so that at least one of them isn’t 1 (the default); you also implement viewForZoomingInScrollView: in the scroll view’s delegate to tell the scroll view which of its subviews is to be the scalable view. Typically, you’ll want the scroll view’s entire contents to be scalable, so you’ll have one direct subview of the scroll view that acts as the scalable view, and anything else inside the scroll view will be a subview of the scalable view, so as to be scaled together with it.

To illustrate, let’s return to the first example in this chapter, where we created a scroll view containing 30 labels. To make this scroll view zoomable, we’ll need to modify the way we create it. As it stands, the scroll view’s subviews are just the 30 labels; there is no single view that we would scale in order to scale all the labels together. So instead of making the 30 labels subviews of the scroll view, we’ll make them subviews of a single scalable view and make the scalable view the subview of the scroll view:

UIScrollView* sv = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; self.view = sv; UIView* v = [[UIView alloc] init]; // scalable view CGFloat y = 10; for (int i=0; i<30; i++) { UILabel* lab = [[UILabel alloc] init]; lab.text = [NSString stringWithFormat:@"This ...

Get Programming iOS 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.