extension Reactive where Base: UIScrollView {
var reachedBottom: ControlEvent<Void> {
let observable = contentOffset
.flatMap { [weak base] contentOffset -> Observable<Void> 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实现加载更多数据了
网友评论