cs193p – Assignment #4 Task #6

Please note, this blog entry is from a previous course. You might want to check out the current one.

When a photo first appears, and as the bounds of the UIScrollView showing a photo change (due to, for example, autorotation), you must adjust the zooming to show as much of the photo as possible with no extra, unused space. Once the user starts pinching to zoom on a given photo, you can stop doing this automatic zooming while that photo continues to be visible.

Like described in lecture #9 viewDidLayoutSubviews is for such occasions the proper place. First calculate which scale would be needed to either show the complete width, or the complete height of the photo. Then pick the higher one because the smaller one would show unused space:

- (void)viewDidLayoutSubviews
    [super viewDidLayoutSubviews];
    double wScale = self.scrollView.bounds.size.width / self.imageView.image.size.width;
    double hScale = self.scrollView.bounds.size.height / self.imageView.image.size.height;
    if (wScale > hScale) self.scrollView.zoomScale = wScale;
    else self.scrollView.zoomScale = hScale;

The complete code is available on github.


Leave a Reply

Your email address will not be published.