-1

I met this weird problem when I had an UIScrollView with pagingEnable = YES and a very large contentSize (let's say over 20000000).

Basically I want to write a PDF viewer just like the iBooks (showing one page in the screen). So the bounds of the UIScrollView is just the size of the screen, but the contentSize will be "the page number of the PDF" * "page width". This worked for small PDF, but with large PDF, the paging function seems broken.

For example, I had a 94MB PDF with over 20000 pages, the width of contentSize will be over 20000000. For the first 3000 pages (approximately), the paging works fine: the scrollview always bounces the page to the center of screen. But after 3000 pages, you will find that the bouncing becomes slow, not that smooth. And start from some page, the bouncing totally broken: the page will not show in the center, but stuck at somewhere else, just like pagingEnable = NO. It doesn't bounce anymore.

At first I thought it was something wrong with my code, but I suprisingly find that the iBooks has the same problem! I can't even trigger the toolbar with a single tap after scrolling the last page. So I'm wondering is this an iOS bug?

More Info: When debugging, I found that after the finger touched up, -scrollViewDidScroll: gets called for many times, which is normal, because the UIScrollView starts bouncing when pagingEnable = YES. But the problem is that the -scrollViewDidEndDecelerating: never gets called. Seems like the bouncing animation is broken at some middle point. Weird.

Keyser
  • 21
  • 4
  • Or, you could use UICollectionView, break the PDF up, and only have a page or so in memory at a time... – Mick MacCallum Feb 08 '14 at 03:56
  • Well, turning to UICollectionView or UITableView may be a choice, but need lots of refactoring. And actually, I don't think memory is the root cause, since the PDF is only 94MB. There must be something wrong with the bouncing function of UIScrollView. – Keyser Feb 08 '14 at 04:58
  • I also test several PDF readers in the App Store: Adobe Reader, PDF Reader, PDF Pro etc. And all of them have the same problem with single page mode. For example, with Adode Reader, the continuous mode is ok, but not with single page mode. So personally I think it's an UIScrollView bug related with pagingEnabled (and bouncing). – Keyser Feb 08 '14 at 05:47

1 Answers1

0

the PDF is only 94MB

File size does not equal the size in memory. PDFs are compressed, but they have to be uncompressed for display.

What's also important is that you don't need a scroll view that's as wide as the entire book - you only need a scroll view that's three pages wide. This is because a) you're hopefully already recycling your page views, and b) because only one page is visible on the screen at any one time you can move the content around as and when you need to.

Is there any reason you are not using a UIPageViewController, which is designed pretty much for this purpose?

Bottom line: there's no reason for your scroll view to be that wide. It only need to be a few pages wide, and you can move the views around to give the illusion the scroll view is much bigger than it actually is. If you search StackOverflow you'll almost certainly find a number of answers that do basically this.

lxt
  • 31,146
  • 5
  • 78
  • 83
  • Thanks, @lxt. I know you are right about the memory and the contentSize, and I'm just about to change the width of contentSize to 3 pages wide. But I'm still very curious about this bug, because all of the PDF readers I tested have this bug, even including iBooks and Adobe Reader. – Keyser Feb 08 '14 at 06:40
  • What's the PDF displaying - just text, or something more graphical? – lxt Feb 09 '14 at 00:17
  • It has both text and images, and it takes about 130mb memory with an iPad2 7.0. iBooks should use less memory than my app, so I don't think this is a memory issue. – Keyser Feb 10 '14 at 06:23
  • Given an iPad 2 only has 512MB of RAM, 130MB of memory is actually quite a lot! – lxt Feb 11 '14 at 05:29
  • Well, I also test it with an iPad3, it costs 130MB of memory, too. I think that's not much for iPad3, right? But the issue is still reproducible. Moreover, memory theory can't explain why the paging works fine with the first 3k pages but failed with the latter pages while the memory stays around 130MB all the time. – Keyser Feb 11 '14 at 09:08