NSScrollView (myScrollView) にラップされた NSView (myView) があります。ズームイン/ズームアウト ボタンを使用して、ユーザーは myView のスケールを変更できます。ユーザーが現在 myView の特定の場所にスクロールしている場合、ズームが行われた後、ビューのその部分を画面上に保持したいと思います。
次のようなコードがあります。
// 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 & 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 < 0)
newOffset.x = 0;
if (newOffset.y < 0)
newOffset.y = 0;
[[myScrollView contentView] scrollToPoint: newOffset];
[myScrollView reflectScrolledClipView: [myScrollView contentView]];
かなり近いように思えますが、完全に正しくはなく、何が間違っているのかわかりません。私の2つの質問は次のとおりです。
1)次のような組み込みのものがありませんか:
[myView adjustScaleBy: 0.5 whilePreservingLocationInScrollview:myScrollView];
2)そうでない場合、上記の「長い道のり」のアプローチで私が間違っていることを誰かが見ることができますか?
ありがとう!