NSScroll 视图中的 contentsize 和 contentOffset 等效

发布于 2024-09-14 12:31:59 字数 126 浏览 1 评论 0原文

我正在将一个应用程序从 iPad 移植到 Mac。 (我知道这听起来很奇怪)

我坚持使用 NSScrollview。请指导我 NSScrollview 中的 contentsize 、 contentOffset 等效项。

I am porting an app from Ipad to mac. (I know that it sounds weird)

I stuck with NSScrollview. Please guide me contentsize , contentOffset equivalent in NSScrollview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧话新听 2024-09-21 12:31:59
UIScrollView* uiScroll;
uiScroll.contentSize;
uiScroll.contentOffset;
uiScroll.contentSize = CGSizeMake(w,h);
uiScroll.contentOffset = CGPointMake(x,y);

=

NSScrollView* nsScroll;
nsScroll.documentView.frame.size;
nsScroll.documentVisibleRect.origin;
nsScroll.documentView.frameSize = NSMakeSize(w,h);
[nsScroll.documentView scrollPoint:NSMakePoint(x,y)];

或者甚至更好:

import AppKit

extension NSScrollView {
    var documentSize: NSSize {
        set { documentView?.setFrameSize(newValue) }
        get { documentView?.frame.size ?? NSSize.zero }
    }
    var documentOffset: NSPoint {
        set { documentView?.scroll(newValue) }
        get { documentVisibleRect.origin }
    }
}

注意:我使用“documentSize”(和“documentOffset”),因为“contentSize”与 NSScrollView 已有的属性冲突。

UIScrollView* uiScroll;
uiScroll.contentSize;
uiScroll.contentOffset;
uiScroll.contentSize = CGSizeMake(w,h);
uiScroll.contentOffset = CGPointMake(x,y);

=

NSScrollView* nsScroll;
nsScroll.documentView.frame.size;
nsScroll.documentVisibleRect.origin;
nsScroll.documentView.frameSize = NSMakeSize(w,h);
[nsScroll.documentView scrollPoint:NSMakePoint(x,y)];

Or perhaps even better:

import AppKit

extension NSScrollView {
    var documentSize: NSSize {
        set { documentView?.setFrameSize(newValue) }
        get { documentView?.frame.size ?? NSSize.zero }
    }
    var documentOffset: NSPoint {
        set { documentView?.scroll(newValue) }
        get { documentVisibleRect.origin }
    }
}

Notes: I used 'documentSize' (and 'documentOffset') because 'contentSize' conflicts with an already existing property of NSScrollView.

终弃我 2024-09-21 12:31:59

除了 @aepryus 中的行之外,这里还有一些更有用的行,用于在 macOS 上获取/设置滚动偏移量:

//Get the current scroll offset:
_contentViewOffset = scrollView.contentView.bounds.origin;

//Set the scroll offset from the retrieved point:
NSPoint scrollPoint = [scrollView.contentView convertPoint:_contentViewOffset toView:scrollView.documentView];
[scrollView.documentView scrollPoint:scrollPoint];

In addition to the lines from @aepryus, here are a couple more useful lines for getting/setting the scroll offset on macOS:

//Get the current scroll offset:
_contentViewOffset = scrollView.contentView.bounds.origin;

//Set the scroll offset from the retrieved point:
NSPoint scrollPoint = [scrollView.contentView convertPoint:_contentViewOffset toView:scrollView.documentView];
[scrollView.documentView scrollPoint:scrollPoint];
短暂陪伴 2024-09-21 12:31:59

您需要了解的有关 NSScrollView 的所有信息都在 Cocoa 滚动视图编程指南 文档中提供。

虽然似乎没有直接的等效项,但 UIScrollViewcontentSize 可以比作 NSScrollView的大小documentView,它是通过 setDocumentView: 作为 NSView 提供给 NSScrollView 的可滚动内容。

setContentOffset: 可以与 NSViewscrollPoint: 进行比较,后者使用 NSPoint 来指定内容的偏移量NSScrollView 中的 documentView

请参阅文档以了解详细说明和代码示例。

Everything you need to know about NSScrollView is laid out in the Scroll View Programming Guide for Cocoa provided in the documentation.

Although it doesn't appear that there's a direct equivalent, UIScrollView's contentSize can be likened to the size of NSScrollView's documentView, which is the scrollable content provided as an NSView to NSScrollView with setDocumentView:.

setContentOffset: can be compared to NSView's scrollPoint:, which uses an NSPoint to specify the offset of the documentView within the NSScrollView.

See the documentation for elaboration and code examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文