TAGS :Viewed: 5 - Published at: a few seconds ago

[ Nested UIScrollViews of PDFs- Uniform Zooming ]

I'm trying to get a UIScrollView that contains a few UIScrollViews of PDFs.

The same idea as this view in the App Store (just with PDFs and the ability to zoom): App Store Screenshot Viewer

I'm having difficulty getting the zoom to work correctly. How can I have them zoom together at the same time?

How can I achieve this when I can only return one view from this method:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView


Answer 1

Watch the WWDC 2011 videos, especially "Advanced ScrollView Techniques" Session 104 and if you like the 104 Session from 2010 too. They both handle the aspect of zooming and scrolling with images. Adopting an pdf view (from another tutorial) shouldn't be a big deal.

In your viewForZoomingInScrollView: method you can call the other scrollView and set a zoom scale programmatically

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (scrollView == firstSV) {
        return viewForFirstScrollView;
    } else {
        return viewForSecondScrollView;

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    firstSV.zoomScale = secondSV.zoomScale;
    // and the other way if you can zoom the second scrollView too

Answer 2

You should return a container UIView to the viewForZoomingInScrollView: delegate method. In this container view you should put all your PDF views side-by-side, as you would images. Now when you zoom in, the container view will be zoomed along with its contents. Boom, done.

Except you have to build a zooming PDF view, and make sure you do not load too many pages at the same time to avoid using too much memory. That's when it gets tricky.

Building a zooming PDF view means either building a view that contains an image that you will render with PDFKit and refresh every time it is zoomed in (the scroll view delegate tells you that), or you will have to use CATiledLayer to render the PDF pages as tiles, saving memory and improving performance.

Not loading too many pages means you will have to add and remove your PDF pages views in / from the container view as the scroll view scrolls.

I did both for a client and it takes forever to get right and bug free (about 3 weeks at least if you're not familiar with the scroll view or PDFKit). See this Stack Overflow question to get you started if you feel like building it.

The other option is to buy a PSPDFKit license which is $800 and about 2 hours of integration to get it to work within your app. After having built a PDF reader myself, I'd choose the second option. It will save you time and money.