title: Unwanted scrolling when animating zoomScale in UIScrollView
body: <p><strong>Executive Summary:</strong>
  At times <code>UIScrollView</code> makes an unwanted change to the value of <code>contentOffset</code>, thus causing the app to display the wrong location in the document being viewed.  The unwanted change happens in conjunction to an animated change to the scroll view's <code>zoomScale</code>.</p>
  
  <p><strong>The Details:</strong>
  I'm having trouble when zooming out with <code>CATiledLayer</code> in a <code>UIScrollView</code>.  The <code>CATiledLayer</code> holds a pdf, and when <code>contentOffset</code> is within a certain range, when I zoom out, the <code>contentOffset</code> is changed (that's the bug) before the zooming occurs.  The <code>contentOffset</code> seems to be changed in Apple's code.</p>
  
  <p>To illustrate the problem, I modified Apple's sample app, ZoomingPDFViewer.  The code is on github: <a href="https://github.com/DirkMaas/ZoomingPDFViewer-bug" rel="noreferrer">https://github.com/DirkMaas/ZoomingPDFViewer-bug</a></p>
  
  <p>A tap will cause <code>zoomScale</code> to be changed to 0.5, using <code>animateWithDuration</code>, thus zooming out.  If the <code>UIScrollView</code>'s <code>contentOffset.y</code> is less than about 2700 or greater than 5900, the <code>zoomScale</code> animation works fine.  If the tap happens when <code>contentOffset.y</code> is between those two values, the <code>contentOffset.y</code> will jump (not animated) to about 2700, and then the <code>zoomScale</code> animation will occur, but scrolling will occur at the same time, so that when the animation is done, the <code>contentOffset.y</code> is where it should be.  But where does the jump come from?</p>
  
  <p>For example, say the <code>contentOffset.y</code> is 2000 when the screen is tapped: the <code>zoomScale</code> animation works just fine; <code>contentOffset.y</code> is not changed.</p>
  
  <p>But if the <code>contentOffset.y</code> is 4000 when the screen is tapped: the <code>contentOffset.y</code> will jump, without animation, to about 2700, and then zooming and scrolling will begin from that point and occur at the same time.  When the animation is done, it looks as if we zoomed straight back from 4000, so we end up in the right place, but the behavior is wrong.</p>
  
  <p>A note on the UI:</p>
  
  <ul>
  <li>the text can be scrolled vertically in the normal way</li>
  <li>the text can be zoomed in and out by pinching in the normal way</li>
  <li>a single tap will cause the <code>zoomScale</code> to be set to 0.5; the change is animated</li>
  </ul>
  
  <p>I've noticed that if <code>zoomScale</code> is greater than 0.5, the jump is not so big.  Also, if I use <code>setZoomScale:animated:</code> instead of <code>animateWithDuration</code>, the bug disappears, but I can't use it because I need to chain animations.</p>
  
  <p>Here is a summary of what I did (the code in github includes these changes):</p>
  
  <ul>
  <li>Downloaded ZoomingPDFViewer from
  <a href="http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html" rel="noreferrer">http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html</a> and opened it in XCode</li>
  <li>Changed Build Settings | Architectures | Base SDK to Latest iOS (iOS 4.3) changed Build Settings | GCC 4.2 - Language | Compile Sources As to Objective-C++ </li>
  <li>removed TestPage.pdf from the project </li>
  <li>added "whoiam 5 24 cropped 3-2.pdf" to the project in its place </li>
  <li>added <code>PDFScrollView *scrollView;</code> to <code>ZoomingPDFViewerViewController</code> class</li>
  <li>changed <code>loadView</code> in <code>ZoomingPDFViewerViewController</code> to initialize <code>scrollView</code> instead of <code>sv</code></li>
  <li>added <code>viewDidLoad</code>, <code>handleTapFrom:recognizer</code> and <code>zoomOut</code> to <code>ZoomingPDFViewerViewController</code> in PDFScrollview.m</li>
  <li>commented out <code>scrollViewDidEndZooming:withView:atScale</code> and <code>scrollViewWillBeginZooming:withView:</code> because they do stuff in the image background that distracts from the issue at hand</li>
  </ul>
  
  <p>Thanks so much for bearing with me, and any and all help!</p>