Created
November 17, 2017 12:16
-
-
Save acalism/66ae5f84c36230f1e4fde29ec55f6604 to your computer and use it in GitHub Desktop.
UIPageViewController 经验总结
This file contains hidden or 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
| extension UIPageViewController { | |
| var bk_scrollView: UIScrollView? { | |
| if let v = view as? UIScrollView { | |
| return v // 事实上,view不是UIScrollView类型 | |
| } | |
| for v in view.subviews where v is UIScrollView { | |
| return v as? UIScrollView // view.subviews 只有一个元素,其类型是 _UIQueuingScrollView,是 UIScrollView 的子类 | |
| } | |
| return nil | |
| } | |
| } | |
| // bk_scrollView.delegate 是 nil,所以可以监控其滚动进度 | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| if nil == pageViewController.bk_scrollView?.delegate { | |
| pageViewController.bk_scrollView?.delegate = self // 监控滚动状态 | |
| } | |
| } | |
| func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
| //print(scrollView.contentSize.width, scrollView.contentOffset.x) | |
| let w = view.bounds.width | |
| let x = scrollView.contentOffset.x | |
| guard x != w else { | |
| return | |
| } | |
| let p = x / w - 1 // 翻一页的进度百分比 | |
| print(p) | |
| // 几个事实(假定单页左右滚动): | |
| // 1. scrollView.contentSize始终有三页宽 | |
| // 2. 当前页处于三页的中间一页,每次翻页结束后重置偏移为,scrollView.contentOffset.w = view.bounds.width,可见子页面的原点x也发生了相应的改变 | |
| // 综上,上面的p会有正负变化,且绝对值不大于1,正负表征滚动方向 | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment