How to maintain the scroll position in NSScrollView when changing scale?

后端 未结 3 1299
心在旅途
心在旅途 2020-12-24 03:22

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 scrolle

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 04:04

    Me thinks you like to type too much… ;-)

    // instead of this:
    NSPoint oldCenter = NSPointFromCGPoint(CGPointMake(oldVisibleRect.origin.x +
        (oldVisibleRect.size.width  / 2.0),
    
    // use this:
    NSPoint oldCenter = NSMakePoint(NSMidX(oldVisibleRect), NSMaxY(oldVisibleRect));
    
    // likewise instead of this:
    [self scaleUnitSquareToSize:NSSizeFromCGSize(CGSizeMake(0.5, 0.5))];
    
    // use this:
    [self scaleUnitSquareToSize:NSMakeSize(0.5, 0.5)];
    
    // and instead of this
    NSPoint newOffset = NSPointFromCGPoint(CGPointMake(
        (oldCenter.x * 0.5) - (newVisibleRect.size.width  / 2.0),
        (oldCenter.y * 0.5) - (newVisibleRect.size.height / 2.0)));
    
    // use this:
    NSPoint newOffset NSMakePoint(
        (oldCenter.x - NSWidth(newVisibleRect)) / 2.f,
        (oldCenter.y - NSHeight(newVisibleRect)) / 2.f);
    

提交回复
热议问题