Mac OSX NSScrollerView 监听滚动偏移量

在iOS中大量使用UITableView控件,用于列表展示,同时需要根据列表滚动的偏移量做一些动画,比如头部放大缩小,下拉刷新等效果,这些都可以通过UIScrollerViewdelegate实时监听到滚动的偏移量,并做相应的处理! 而在Mac OSX 平台这一切都得你自己来写,你可以进入NSScrollerView的头文件查看,它并没有delegate属性,所有我们需要自己是实现
NSScrollerView 结构图
//
// BaseScrollView.swift
// WeiboOS
//
// Created by yuanmaochao on 16/8/29.
// Copyright © 2016年 king. All rights reserved.
//
​
import Cocoa
​
/// 通过协议 将 contentOffset 传递出去
/// 协议遵守 NSObjectProtocol 协议 就可以调用 respondsToSelector 方法判断代理是否实现了代理方法 并且 delegate 属性 才能使用 weak 修饰
@objc protocol BaseScrollViewDelegate: NSObjectProtocol {
   
   optional func scrollViewDidContentOffsetChange(scrollView: BaseScrollView, contentOffset: NSPoint)
}
​
class BaseScrollView: NSScrollView {
   
   weak var delegate: BaseScrollViewDelegate?
   
   /// 重载初始化方法
   override init(frame frameRect: NSRect) {
       super.init(frame: frameRect)
       setup()
   }
   
   required init?(coder: NSCoder) {
       super.init(coder: coder)
       setup()
   }
   
   func setup() {
       /// 需要设置 self.contentView 可以发送 Bounds 改变通知
       self.contentView.postsBoundsChangedNotifications = true
       /// 监听通知
       /// 需要注意 object: 必须要传 self.contentView 如果传nil 
       /// 其他 NSScrollerView 控件拖动也会监听到
       notificationCenter.addObserver(self,
                                       selector: #selector(BaseScrollView.synchronizedViewContentBoundsDidChange(_:)),
                                       name: NSViewBoundsDidChangeNotification,
                                       object: self.contentView)
   }
   
   func synchronizedViewContentBoundsDidChange(note: NSNotification) {
       
       /// self.contentView 是一个 NSClipView 控件
       /// self.contentView.documentVisibleRect 可见范围 相当于 UIScrollerView 的 contentSize
       
       /// 判断delegate 是否有值 并判断是否实现了相关代理方法
       if let delegate = delegate {
           if delegate.respondsToSelector(#selector(BaseScrollViewDelegate.scrollViewDidContentOffsetChange(_:contentOffset:))) {
               delegate.scrollViewDidContentOffsetChange!(self, newOffset: self.contentView.documentVisibleRect.origin)
           }
       }
       
       if NSEqualPoints(self.contentView.bounds.origin, self.contentView.documentVisibleRect.origin) == false {
           self.contentView.scrollToPoint(self.contentView.documentVisibleRect.origin)
           self.reflectScrolledClipView(self.contentView)
       }
       self.enclosingScrollView
   }
​
   func stopSynchronizing() {
       
       notificationCenter.removeObserver(self,
                                         name: NSViewBoundsDidChangeNotification,
                                         object: self.contentView)
       
   }
   
   deinit {
    /// 销毁时 移除通知
       stopSynchronizing()
   }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • 投资的核心方法论,低买高卖。说起来简单,试试就知道了,真的很难做到,要多难就有多难。 一切“世人皆知的秘密...
    李涛25班阅读 191评论 0 0
  • 20170714 周五(2) [玫瑰]看到希望 感到快乐 自主自控[玫瑰] 上午孩子没有课,来我单位写作业。孩子也...
    每天都微笑阅读 161评论 0 0
  • 小米3真机调试这个错误指安装的时候被用户取消了1.确保手机处于开发者模式中2.允许通过USB安装应用(我就是这个原...
    JAG阅读 1,157评论 1 0