Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically restore the UIScrollView's zoom level and position when my app relaunches?

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?

like image 536
jpm Avatar asked Jan 23 '23 18:01

jpm


2 Answers

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

like image 115
amattn Avatar answered Jan 29 '23 07:01

amattn


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/.

like image 42
Andrey Tarantsov Avatar answered Jan 29 '23 08:01

Andrey Tarantsov