Objective-c 中 NSScrollView 中的缩放图像
我有两个 NSScrollView 的滚动彼此同步(使用 Apple 的示例)并且它正在运行。接下来,我实现了一个缩放功能,可以缩放两个图像(根据定义它们具有相同的大小)。缩放代码是使用 中的代码进行调整的这里,我在这里介绍:
if (oldZoomValue > [sender floatValue]) // then we are zooming in
{
float zoomFactor = 1 + oldZoomValue - [sender floatValue];
oldZoomValue = [sender floatValue];
NSRect visible = [synchroS_ScrollView documentVisibleRect];
NSRect newrect = NSInsetRect(visible, NSWidth(visible)*(1 - 1/zoomFactor)/2.0, NSHeight(visible)*(1 - 1/zoomFactor)/2.0);
NSRect frame = [synchroS_ScrollView.documentView frame];
[synchroS_ScrollView.documentView scaleUnitSquareToSize:NSMakeSize(zoomFactor, zoomFactor)];
[synchroS_ScrollView.documentView setFrame:NSMakeRect(0, 0, frame.size.width * zoomFactor, frame.size.height * zoomFactor)];
[[synchroS_ScrollView documentView] scrollPoint:newrect.origin];
NSRect visibleI = [synchroI_ScrollView documentVisibleRect];
NSRect newrectI = NSInsetRect(visibleI, NSWidth(visibleI)*(1 - 1/zoomFactor)/2.0, NSHeight(visibleI)*(1 - 1/zoomFactor)/2.0);
NSRect frameI = [synchroI_ScrollView.documentView frame];
[synchroI_ScrollView.documentView scaleUnitSquareToSize:NSMakeSize(zoomFactor, zoomFactor)];
[synchroI_ScrollView.documentView setFrame:NSMakeRect(0, 0, frameI.size.width * zoomFactor, frameI.size.height * zoomFactor)];
[[synchroI_ScrollView documentView] scrollPoint:newrectI.origin];
}
else
{
// the equivalent but for zoom-out
}
如果我在没有同步滚动的情况下使用缩放功能,则 NSScrollView 上的两个图像都会按预期很好地缩放。然而,如果它们是同步的,程序就会崩溃,它似乎是几个调用的堆栈:在主线程上,它显示了很多类似的东西
#18 0x0000000100003e37 in -[SynchroScrollView synchronizedViewContentBoundsDidChange:] ()
,在GDB上:
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
sharedlibrary apply-load-rules all
[Switching to process 33162 thread 0x0]
[Switching to process 33162 thread 0xac03]
[Switching to process 33162 thread 0x903]
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
...
(gdb)
任何人都可以给我指出调试的方向或有任何想法吗?怎么了? 感谢您的帮助。
I have two NSScrollView's with scroll synchronized with each other (using Apple's example) and it is working. Next I implemented a zoom that would zoom both images (they have the same size by definition). The code for the zoom was adapted using code from here, which I present here:
if (oldZoomValue > [sender floatValue]) // then we are zooming in
{
float zoomFactor = 1 + oldZoomValue - [sender floatValue];
oldZoomValue = [sender floatValue];
NSRect visible = [synchroS_ScrollView documentVisibleRect];
NSRect newrect = NSInsetRect(visible, NSWidth(visible)*(1 - 1/zoomFactor)/2.0, NSHeight(visible)*(1 - 1/zoomFactor)/2.0);
NSRect frame = [synchroS_ScrollView.documentView frame];
[synchroS_ScrollView.documentView scaleUnitSquareToSize:NSMakeSize(zoomFactor, zoomFactor)];
[synchroS_ScrollView.documentView setFrame:NSMakeRect(0, 0, frame.size.width * zoomFactor, frame.size.height * zoomFactor)];
[[synchroS_ScrollView documentView] scrollPoint:newrect.origin];
NSRect visibleI = [synchroI_ScrollView documentVisibleRect];
NSRect newrectI = NSInsetRect(visibleI, NSWidth(visibleI)*(1 - 1/zoomFactor)/2.0, NSHeight(visibleI)*(1 - 1/zoomFactor)/2.0);
NSRect frameI = [synchroI_ScrollView.documentView frame];
[synchroI_ScrollView.documentView scaleUnitSquareToSize:NSMakeSize(zoomFactor, zoomFactor)];
[synchroI_ScrollView.documentView setFrame:NSMakeRect(0, 0, frameI.size.width * zoomFactor, frameI.size.height * zoomFactor)];
[[synchroI_ScrollView documentView] scrollPoint:newrectI.origin];
}
else
{
// the equivalent but for zoom-out
}
If I use the zoom functionality without synchronized Scroll's, both images on the NSScrollView are zoomed nicely as expected. However, if they are synchronized, the program crashes with it seems to be a stack of several calls: on the main thread, it shows a lot of things like
#18 0x0000000100003e37 in -[SynchroScrollView synchronizedViewContentBoundsDidChange:] ()
and on the GDB:
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
sharedlibrary apply-load-rules all
[Switching to process 33162 thread 0x0]
[Switching to process 33162 thread 0xac03]
[Switching to process 33162 thread 0x903]
warning: Unable to restore previously selected frame.
warning: Unable to restore previously selected frame.
...
(gdb)
Can anyone point me a direction for debug or have any idea what is happening?
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你在哪里调用了缩放scrollView的代码,但也许缩放scrollView A会触发缩放scrollView B,这又会再次触发缩放scrollView A,从而形成无限循环。更改
synchronizedViewContentBoundsDidChange
方法中的边界会一次又一次地调用相同的方法。我知道这个问题已经有几个月了,但这也许有帮助:)
I'm not sure where you call the code for zooming the scrollView, but maybe zooming in scrollView A triggers zooming in scrollView B, which in turn triggers zooming in scrollView A again, creating an endless loop. changing the bounds in the
synchronizedViewContentBoundsDidChange
method would call the same method again and again.I know this question is a few months old, but maybe this helps :)