Ios – UIScrollView image/photo viewer with paging enabled and zooming

iosiphoneuiscrollview

OK, I think it's time to make an official place on the internet for this problem: How to make a UIScrollView photoviewer with paging and zooming. Welcome my fellow UIScrollView hackers.

I have a UIScrollView with paging enabled, and I'm displaying UIImageViews like the built-in photos app. (Does this sound familiar yet?)

I found the following project on github:

https://github.com/andreyvit/ScrollingMadness/wiki

Which shows how to implement zooming in a scroll view while paging is enabled. If anyone else tries this out, I actually had to remove the UIScrollView subclass and use the native class otherwise it doesn't work. I think it's because of changes in the 3.0 SDK relating to how the scroll view intercepts touch events.

So the the idea is to remove all the other views when you start zooming, and move the current view to (0, 0) in the scrollview, updating the contentsize etc. Then when you zoom back to 1.0f it adds the other views back and puts things all back in order.

Anyway, that project works perfectly in the simulator, but on the device there is some nasty movement of the view you are resizing, which looks like it's caused by the fact we are changing the contentsize/offset etc. for the view being resized. You have to do this view moving otherwise you can pan left through the whitespace left by the other views.

I found one interesting note in the "Known Issues" of the 3.0 SDK release notes:

UIScrollView: After zooming, content inset is ignored and content is left in the wrong position.

This kind of sounds like what is happening here. After zooming in, the view will shift offscreen because you have changed the offset etc.

I've spent hours on this already and I'm slowing coming to the sad realization that this just isn't going to work.

Three20's photo viewer is out of the question: it's too heavy weight and there is too much unnecessary UI and other behaviour.

The built in Photo app seems to do some magic. If you zoom in on an image and pan to the far edges, the current photo moves independently of the photo next to it which isn't what you get when trying this with a standard UIScrollView.

I've seen discussion about nesting the UIScrollView's but I really don't want to go there.

Has anybody managed this with the standard UIScrollView (and works in the 2.2 and 3.0 SDK)? I don't fancy rolling my own zoom + bounce + pan + paging code.

Best Answer

UPDATE

I deleted my previous answer because of the news below...

Big news for those who haven't heard. Apple has released the 2010 WWDC session videos to all members of the iphone developer program. One of the topics discussed is how they created the photos app!!! They build a very similar app step by step and have made all the code available for free.

It does not use private api either. Here is a link to the sample code download. You will probably need to login to gain access.

Check This

And, here is a link to the iTunes WWDC page:

Check This