Skip to content

Instantly share code, notes, and snippets.

@Mukeshawal
Created January 28, 2020 08:12
Show Gist options
  • Save Mukeshawal/57ba102e51d5f54fab32a2e997480da0 to your computer and use it in GitHub Desktop.
Save Mukeshawal/57ba102e51d5f54fab32a2e997480da0 to your computer and use it in GitHub Desktop.
override func didMoveToSuperview() {
super.didMoveToSuperview()
addObserver(.contentOffset)
addObserver(.panGesture)
}
//MARK: - observers
/// Add scroll view content y offset and scroll view panGesture observers
/// - Parameter observer: RefreshControl.Observer type
private func addObserver(_ observer : Observer){
guard let scrollView = containerScrollView else {return}
switch observer {
case .contentOffset:
scrollView.rx.contentOffset.bind(onNext: {[weak self] offset in
self?.scrollViewContentYOffset = -offset.y
self?.setNeedsDisplay()
}).disposed(by: bag)
break
case .panGesture:
scrollView.panGestureRecognizer.rx.event.bind(onNext: {[weak self] panGesture in
self?.xPositionOfPan = panGesture.location(in: self?.containerScrollView).x
self?.setNeedsDisplay()
switch panGesture.state{
case .cancelled,.failed,.ended:
if (self?.scrollViewContentYOffset)! > (self?.thresholdDrag)!{
self?.refreshingStatus = true
self?.setNeedsDisplay()
self?.animateRefreshCircle()
self?.onRefreshing()
}
break
default:
break
}
}).disposed(by: bag)
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment