Created
April 29, 2018 15:21
-
-
Save kishikawakatsumi/227ad300f0704f9323984265150ce0d8 to your computer and use it in GitHub Desktop.
UIScrollView + UIPageControl with RxSwift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import RxCocoa | |
fileprivate extension Reactive where Base: UIScrollView { | |
fileprivate var currentPage: Observable<Int> { | |
return didEndDecelerating.map({ | |
let pageWidth = self.base.frame.width | |
let page = floor((self.base.contentOffset.x - pageWidth / 2) / pageWidth) + 1 | |
return Int(page) | |
}) | |
} | |
} | |
fileprivate extension UIScrollView { | |
fileprivate func setCurrentPage(_ page: Int, animated: Bool) { | |
var rect = bounds | |
rect.origin.x = rect.width * CGFloat(page) | |
rect.origin.y = 0 | |
scrollRectToVisible(rect, animated: animated) | |
} | |
} | |
class ViewController: UIViewController { | |
private let disposeBag = DisposeBag() | |
@IBOutlet weak var scrollView: UIScrollView! | |
@IBOutlet weak var pageControl: UIPageControl! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
pageControl.rx.controlEvent(.valueChanged) | |
.subscribe(onNext: { [weak self] in | |
guard let currentPage = self?.pageControl.currentPage else { | |
return | |
} | |
self?.scrollView.setCurrentPage(currentPage, animated: true) | |
}) | |
.disposed(by: disposeBag) | |
scrollView.rx.currentPage | |
.subscribe(onNext: { [weak self] in | |
self?.pageControl.currentPage = $0 | |
}) | |
.disposed(by: disposeBag) | |
scrollView.isPagingEnabled = true | |
scrollView.contentSize = CGSize(width: scrollView.bounds.width * 4, height: scrollView.bounds.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can also be bound as: