使用RxSwift扩展UISrollView到达底部触发的事件

extension Reactive where Base: UIScrollView {
    var reachedBottom: ControlEvent {
        let observable = contentOffset
            .flatMap { [weak base] contentOffset -> Observable in
                guard let scrollView = base else {
                    return Observable.empty()
                }
                let visibleHeight = scrollView.frame.height - scrollView.contentInset.top - scrollView.contentInset.bottom
                let y = contentOffset.y + scrollView.contentInset.top
                let threshold = max(0.0, scrollView.contentSize.height - visibleHeight)
                return y > threshold ? Observable.just() : Observable.empty()
            }
        return ControlEvent(events: observable)
    }
}

这样就可以愉快地配合RxSwift实现加载更多数据了

你可能感兴趣的:(使用RxSwift扩展UISrollView到达底部触发的事件)