How to dynamically restore the zoom level and position of a UIScrollView when resuming my application?

My application consists of a UIImageView inside a UIScrollView , and I am showing a large image inside it. Scrolling allows the user to pinch to zoom in / out, and everything seems to be working fine.

However, when my application terminates and then restarts, UIScrollView displays the image again at its original zoom level (which is currently set to display the entire image by scaling it in the Aspect Approach mode).

I would really like to restart my application and reopen UIScrollView with the same parameters that were set when the application terminated. Therefore, if my image is currently maximized and scrolls to the very bottom, this should be a view when I reopen the application.

How can i do this?

+4
source share
4 answers

I found a way to programmatically increase the UIScrollView. This can help you set the desired zoom level at startup.

Sample code along with the ZoomScrollView class, which encapsulates the required scaling magic and a detailed discussion of how (and why) UIScrollView scaling works, is available at github.com/andreyvit/ScrollingMadness/ .

+2
source

Check the .transform property of the view. If you are enlarged, this must be changed. Save and restore it the next time you start.

+3
source

Someone asked about this a while ago, but I don’t think you will like the answer . Hope you get the best option.

+2
source

I do it like this: When you exit the zoom mode (myScrollview.zoomScale) and the content frame source.

When you reopen, you can set the zoom. You should also set the content size for the new zoom level, because otherwise the borders are wrong.

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

To set the offset:

 [myScrollView setContentOffset:savedFrameOrigin animated:YES]; 
+1
source

All Articles