objective-c – 两个NSScrollView实例之间的同步滚动

objective-c – 两个NSScrollView实例之间的同步滚动,第1张

概述我有两个NSScrollView实例都呈现相同内容的视图.然而,第二滚动视图具有在第一滚动视图中呈现的文档视图的缩小版本.宽度和高度都可以单独缩放,原始宽度 – 高度约束可能会丢失,但这并不重要. 我有同步滚动工作,甚至考虑到第二个滚动视图需要根据缩放对齐其滚动行为.有一点小问题我一直把头发拉过来: >由于两个视图都快乐地沿着较小的视图滚动,需要慢慢赶上较大的视图,以便它们同时“到达”文档的末尾. 我有两个NSScrollVIEw实例都呈现相同内容的视图.然而,第二滚动视图具有在第一滚动视图中呈现的文档视图的缩小版本.宽度和高度都可以单独缩放,原始宽度 – 高度约束可能会丢失,但这并不重要.

@H_419_8@

我有同步滚动工作,甚至考虑到第二个滚动视图需要根据缩放对齐其滚动行为.有一点小问题我一直把头发拉过来:@H_419_8@

>由于两个视图都快乐地沿着较小的视图滚动,需要慢慢赶上较大的视图,以便它们同时“到达”文档的末尾.现在这没有发生,结果是较小的视图位于较大视图之前的“文档结束”.@H_419_8@

同步滚动的代码基于Apple的文档“同步滚动视图”中的示例.我已将synchronizedVIEwContentBoundsDIDChange:改编为以下代码:@H_419_8@

@H_419_8@

- (voID) synchronizedVIEwContentBoundsDIDChange: (NSNotification *) notification {    // get the changed content vIEw from the notification    NSClipVIEw *changedContentVIEw = [notification object];    // get the origin of the NSClipVIEw of the scroll vIEw that    // we're watching    NSPoint changedBoundsOrigin = [changedContentVIEw documentVisibleRect].origin;;    // get our current origin    NSPoint curOffset = [[self contentVIEw] bounds].origin;    NSPoint newOffset = curOffset;    // scrolling is synchronized in the horizontal plane    // so only modify the x component of the offset    // "scale" variable will correct for difference in size between vIEws    NSSize ownSize = [[self documentVIEw] frame].size;    NSSize otherSize = [[[self synchronizedScrollVIEw] documentVIEw] frame].size;    float scale = otherSize.wIDth / ownSize.wIDth;    newOffset.x = floor(changedBoundsOrigin.x / scale);    // if our synced position is different from our current    // position,reposition our content vIEw    if (!NSEqualPoints(curOffset,changedBoundsOrigin)) {        // note that a scroll vIEw watching this one will        // get notifIEd here        [[self contentVIEw] scrolltopoint:newOffset];        // we have to tell the NSScrollVIEw to update its        // scrollers        [self reflectScrolledClipVIEw:[self contentVIEw]];    }}

我将如何更改该代码以实现所需的效果(两个滚动条到达文档的末尾)?@H_419_8@

编辑:一些澄清,因为当我自己回读它时会感到困惑:滚动第一个视图到达结尾时,较小的视图需要减慢速度.这可能意味着重新评估该缩放因子……但是如何?@H_419_8@

编辑2:我根据Alex的建议更改了方法:@H_419_8@

@H_419_8@

NSScroller *myScroll = [self horizontalScroller];NSScroller *otherScroll = [[self synchronizedScrollVIEw] horizontalScroller];//[otherScroll setfloatValue: [myScroll floatValue]];NSLog(@"My scroller value: %f",[myScroll floatValue]);NSLog(@"Other scroller value: %f",[otherScroll floatValue]);// Get the changed content vIEw from the notification.NSClipVIEw *changedContentVIEw = [notification object];// Get the origin of the NSClipVIEw of the scroll vIEw that we're watching.NSPoint changedBoundsOrigin = [changedContentVIEw documentVisibleRect].origin;;// Get our current origin.NSPoint curOffset = [[self contentVIEw] bounds].origin;NSPoint newOffset = curOffset;// Scrolling is synchronized in the horizontal plane so only modify the x component of the offset.NSSize ownSize = [[self documentVIEw] frame].size;newOffset.x = floor(ownSize.wIDth * [otherScroll floatValue]);// If our synced position is different from our current position,reposition our content vIEw.if (!NSEqualPoints(curOffset,changedBoundsOrigin)) {    // Note that a scroll vIEw watching this one will get notifIEd here.    [[self contentVIEw] scrolltopoint: newOffset];    // We have to tell the NSScrollVIEw to update its scrollers.    [self reflectScrolledClipVIEw:[self contentVIEw]];}

使用这种方法,当两个滚动条达到0.7的值时,较小的视图被较大的视图“超越”,这是不好的.较大的视图然后滚动到文档的末尾.@H_419_8@解决方法 我想你可能会以错误的方式接近这一点.我认为你应该得到每个卷轴相对于自身滚动的距离的百分比,并将其应用于另一个视图.如何使用NSScroller的-floatValue这样做的一个例子:

@H_419_8@

@H_419_8@

NSScroller *myScroll = [self verticalScroller];NSScroller *otherScroll = [otherScrollVIEw verticalScroller];[myScroll setfloatValue:otherScroll.floatValue];
总结

以上是内存溢出为你收集整理的objective-c – 两个NSScrollView实例之间的同步滚动全部内容,希望文章能够帮你解决objective-c – 两个NSScrollView实例之间的同步滚动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1072299.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存