Swift - 属性监听器

属性监听器

  • 介绍: 属性监听器, 监听属性的值改变, 就像按钮的点击事件一样来监听其他的值改变

  • 举例: 监听scrollView的contentOffset属性的改变

/// 为scrollView的contentOffset属性, 添加一个监听者
scrollView.addObserver(self, forKeyPath: contentOffset, options: .new, context: nil)

// 监听UIScrollView的contentOffset属性
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        // 如果keyPath是contentOffset, 则调用下面的方法
        if (contentOffset == keyPath) {
            self.adjustStateWithContentOffset()
        }
    }

猜你喜欢

转载自blog.csdn.net/u012678352/article/details/54612485
今日推荐