如何确定 NSScrollView 当前是否正在滚动



如何知道我的 NSScrollView 当前是否正在滚动?在 iOS 上,我可以使用委托,但尽管在谷歌上搜索了很多,但我找不到在 Mac 上执行此操作的方法。

提前感谢!

您可以收到通知 NSViewBoundsDidChangeNotification 如下所示

NSView *contentView = [scrollview contentView];
[contentView setPostsBoundsChangedNotifications:YES];
// a register for those notifications on the content view.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(boundDidChange:)
                                             name:NSViewBoundsDidChangeNotification
                                           object:contentView];

通知方法

- (void)boundDidChange:(NSNotification *)notification {
    // get the changed content view from the notification
    NSClipView *changedContentView=[notification object];
}

从OS X 10.9开始,还有NSScrollView.willStartLiveScrollNotification。它在用户滚动事件开始时发布在主线程上。

例如

NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll), name: NSScrollView.willStartLiveScrollNotification, object: nil)

我的两分钱给OSX mojave/swift 5

 let nc =  NotificationCenter.default
    nc.addObserver(
        self,
        selector: #selector(scrollViewWillStartLiveScroll(notification:)),
        name: NSScrollView.willStartLiveScrollNotification,
        object: scrollView
    )
    nc.addObserver(
        self,
        selector: #selector(scrollViewDidEndLiveScroll(notification:)),
        name: NSScrollView.didEndLiveScrollNotification,
        object: scrollView
    )

....

   @objc func scrollViewWillStartLiveScroll(notification: Notification){
        #if DEBUG
        print("(#function) ")
        #endif
    }

    @objc func scrollViewDidEndLiveScroll(notification: Notification){
        #if DEBUG
        print("(#function) ")
        #endif
    }

最新更新