美文网首页
监听滚动条滚动到底部

监听滚动条滚动到底部

作者: FallPine | 来源:发表于2018-09-17 16:25 被阅读13次
    • 扩展UIScrollView

    import UIKit
    import RxSwift
    import RxCocoa
     
    extension Reactive where Base: UIScrollView  {
         
        //视图滚到底部检测序列
        var reachedBottom: Signal<()> {
            return contentOffset.asDriver()
                .flatMap { [weak base] contentOffset -> Signal<()> in
                    guard let scrollView = base else {
                        return Signal.empty()
                    }
                     
                    //可视区域高度
                    let visibleHeight = scrollView.frame.height - scrollView.contentInset.top
                        - scrollView.contentInset.bottom
                    //滚动条最大位置
                    let threshold = max(0.0, scrollView.contentSize.height - visibleHeight)
                    //如果当前位置超出最大位置则发出一个事件
                    let y = contentOffset.y + scrollView.contentInset.top
                    return y > threshold ? Signal.just(()) : Signal.empty()
            }
        }
    }
    

    参考文章:Swift - RxSwift的使用详解68(监听滚动条滚动到底部的行为:reachedBottom)

    相关文章

      网友评论

          本文标题:监听滚动条滚动到底部

          本文链接:https://www.haomeiwen.com/subject/qbkrnftx.html