【发布时间】:2011-03-28 08:10:30
【问题描述】:
我正在将应用程序从 Ipad 移植到 mac。 (我知道这听起来很奇怪)
我坚持使用 NSScrollview。请指导我 contentsize , contentOffset 在 NSScrollview 中等效。
【问题讨论】:
标签: ios iphone macos uiscrollview nsscrollview
我正在将应用程序从 Ipad 移植到 mac。 (我知道这听起来很奇怪)
我坚持使用 NSScrollview。请指导我 contentsize , contentOffset 在 NSScrollview 中等效。
【问题讨论】:
标签: ios iphone macos uiscrollview 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)];
或者甚至更好:
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 的现有属性冲突。
【讨论】:
nsScroll.documentView.scrollPoint 不可用。它应该是[nsScroll.documentView.scrollPoint NSMakePoint(x,y)]。
UIScrollView.contentOffset 设置器和NSView.scroll(_:) 之间存在细微差别:后者使滚动条闪烁。如果有人发现如何不让它们闪烁,请告知。
除了来自@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];
【讨论】:
您需要了解的有关NSScrollView 的所有信息都在文档中提供的Scroll View Programming Guide for Cocoa 中列出。
虽然看起来没有直接等价物,但UIScrollView 的contentSize 可以比作NSScrollView 的documentView 的大小,这是作为NSView 提供的可滚动内容到NSScrollView 和setDocumentView:。
setContentOffset: 可以与NSView 的scrollPoint: 进行比较,后者使用NSPoint 来指定documentView 在NSScrollView 中的偏移量。
有关详细说明和代码示例,请参阅文档。
【讨论】: