title: How to maintain the scroll position in NSScrollView when changing scale?
body: <p>I've got an NSView (myView) wrapped in an NSScrollView (myScrollView).  Using zoom-in/out buttons, the user can alter the scale of myView.  If the user is currently scrolled to a particular spot in myView, I'd like to keep that part of the view on-screen after the zooming has taken place.</p>
  
  <p>I've got code that looks like this:</p>
  
  <pre><code>    // preserve current position in scrollview
      NSRect oldVisibleRect = [[myScrollView contentView] documentVisibleRect];
      NSPoint oldCenter = NSPointFromCGPoint(CGPointMake(oldVisibleRect.origin.x + (oldVisibleRect.size.width  / 2.0),
                                                         oldVisibleRect.origin.y + (oldVisibleRect.size.height / 2.0)));
  
      // adjust my zoom
      ++displayZoom;
      [self scaleUnitSquareToSize:NSSizeFromCGSize(CGSizeMake(0.5, 0.5))];
      [self calculateBounds];  // make sure my frame &amp; bounds are at least as big as the visible content view
      [self display];
  
      // Adjust scroll view to keep the same position.
      NSRect newVisibleRect = [[myScrollView contentView] documentVisibleRect];
      NSPoint newOffset = NSPointFromCGPoint(CGPointMake((oldCenter.x * 0.5) - (newVisibleRect.size.width  / 2.0),
                                                         (oldCenter.y * 0.5) - (newVisibleRect.size.height / 2.0)));
      if (newOffset.x &lt; 0)
          newOffset.x = 0;
      if (newOffset.y &lt; 0)
          newOffset.y = 0;
  
      [[myScrollView contentView] scrollToPoint: newOffset];
      [myScrollView reflectScrolledClipView: [myScrollView contentView]];
  </code></pre>
  
  <p>And it seems sort of close, but it's not quite right and I can't figure out what I'm doing wrong.  My two questions are:</p>
  
  <p>1) Is there not a built-in something along the lines of:</p>
  
  <pre><code>   [myView adjustScaleBy: 0.5 whilePreservingLocationInScrollview:myScrollView];
  </code></pre>
  
  <p>2) If not, can anyone see what I'm doing wrong in my "long way around" approach, above?</p>
  
  <p>Thanks!</p>