NSScrollView (myScrollView)에 래핑 된 NSView (myView)가 있습니다. 확대/축소 버튼을 사용하여 myView의 배율을 변경할 수 있습니다. 사용자가 현재 myView의 특정 지점으로 스크롤되는 경우, 확대 한 후에 화면의 해당 부분을 화면에 유지하고 싶습니다.스케일을 변경할 때 NSScrollView에서 스크롤 위치를 유지하는 방법은 무엇입니까?
I는 다음과 같습니다 코드를 가지고 :
// 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]];
을 그리고 그것은 일종의 가까이 보이지만, 그것은 아주 옳지 않아 나는 내가 잘못 뭘하는지 알아낼 수 없습니다. 내 두 가지 질문은 다음과 같습니다
1)이 아닌 내장의 라인을 따라 뭔가 :하지 않으면
이[myView adjustScaleBy: 0.5 whilePreservingLocationInScrollview:myScrollView];
2), 사람이 내 "먼 길을 잘못 뭘하는지 볼 수 있습니다 위의 "접근 방식을 사용합니까?
감사합니다.
내가 원하는 결과는 다음과 같습니다. 화면 중간을보고 있습니다. 내가 확대 (또는 -out) 할 때, 화면 중앙에 그 것을 유지하십시오. 감사합니다. – Olie
우수 답변! 계산 된 원점으로 뷰를 스크롤 한 후에 스크롤러에 floatValue를 설정하는 대신 스크롤 뷰에서 reflectScrolledClipView :를 호출했습니다. 이것은 스크롤 바가 올바른 위치에있는 것처럼 보입니다. 이것이 내가 만든 첫 번째 Mac 응용 프로그램이므로 이것이 맞는지 확실하지 않지만 나에게 적합합니다. – afarnham