2

I want to create a scrollView that works exactly like you pan/zoom an image in the Photo app:

-A landscape image is aspect fit on the portrait screen,

-You can zoom into the image,

-If you rotate the device zoomed (landscape), the image remains in the middle,

-And when you zoom back, the image is still aspect fit in the new landscape screen (streched full screen).

So I need aspect fit, and zooming features at once.

I have implemented a solution, where I layout the scrollView's content "by hand" in layouSubviews to have the aspect fit, but that disturbs zooming behaviour.

Is there a neat UIKit way to handle this? Or I have to create my own implementation here?

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

1 Answers1

0

You need to enable zooming (and set the min and max zoom scale) and then implement scrollViewDidZoom. Here's some sample code to get you started that handles centering the image. You can tweak it to do the other parts:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView 
{
    CGSize boundsSize = scrollView.bounds.size;
    CGRect frameToCenter = imageView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageView.frame = frameToCenter;
}

Note: this code assumes you keep a reference to the UIImageView (imageView in this example) in your UIScrollView.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • I don't want to center the image, I want to keep the zoomed part centered only. But the main point is to keep aspect fit, that means the scale of the content changes when user turns the device. Just take a look on the Photo app. – Geri Borbás May 09 '12 at 07:46