NSScrollView检测滚动位置

rea*_*sse 2 cocoa objective-c nstableview nsscrollview

如何在底部滚动时检测位置滚动?

[[_scrollView contentView] setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(boundsDidChangeNotification:)
                                             name:NSViewBoundsDidChangeNotification
                                           object:[_scrollView contentView]];





- (void) boundsDidChangeNotification: (NSNotification *) notification
{
    NSPoint currentScrollPosition = [[_scrollView contentView] bounds].origin;
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*ing 11

您可以使用UIScrollViewcontentView 的可见rect 而不是bounds:

private func configureScrollView() {
        self.scrollView.contentView.postsBoundsChangedNotifications = true
        NotificationCenter.default.addObserver(self, selector: #selector(contentViewDidChangeBounds), name: NSView.boundsDidChangeNotification, object: self.scrollView.contentView)
    }

@objc
func contentViewDidChangeBounds(_ notification: Notification) {
    guard let documentView = scrollView.documentView else { return }

    let clipView = scrollView.contentView
    if clipView.bounds.origin.y == 0 {
        print("bottom")
    } else if clipView.bounds.origin.y + clipView.bounds.height == documentView.bounds.height {
        print("top")
    }
}
Run Code Online (Sandbox Code Playgroud)

内容视图边界在滚动期间不会更改,因此在原始代码中,bounds.origin可能始终返回0/0.


rea*_*sse 5

if (_scrollView.verticalScroller.floatValue > 0.9)
{
    // bottom
    // do something
}
Run Code Online (Sandbox Code Playgroud)