Question

My application consists of a UIImageView inside a UIScrollView, and I display a big image inside of it. The scroll view allows the user to pinch to zoom in/out in the image, and that all seems to work just fine.

However, when my application is terminated and then re-launched, the UIScrollView displays the image again in the original zoom level (which is currently set to display the whole image, by scaling it in a "aspect fit" mode).

I would really like to be able to re-launch my app and have the UIScrollView reopen with the same parameters as it was set when the app terminated. So if my image is currently zoomed in to the max, and scrolled all the way to the bottom left of it, that should be the view when I open the app again.

How can I do this?

Was it helpful?

Solution

I have found a way to programmatically zoom UIScrollView. This may help you set the desired zoom level upon startup.

The sample code, together with ZoomScrollView class that encapsulates the required zooming magic and a detailed discussion of how (and why) UIScrollView zooming works is available at github.com/andreyvit/ScrollingMadness/.

OTHER TIPS

Check the .transform property of the view. If you are zoomed in, this should be modified. Save and restore it on the next launch.

Someone asked this a while ago, but I don't think you'll like the answer. Hopefully you'll get a better one.

I do it like this: When you quit save the zoomlevel (myScrollview.zoomScale) and the contentview frame origin.

When you reopen you can set the zoomscale. You also have to set the content size to the new zoomlevel, because otherwise the boundaries are not correct.

    [myScrollview setZoomScale:savedZoomScale];
    CGSize newsize = CGSizeMake(origsize.width * savedZoomScale, 
                                 origsize.height * savedZoomScale);
    myScrollview.contentSize = newsize;

To set the offset:

[myScrollView setContentOffset:savedFrameOrigin animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top