Get scroll position of UIPageViewController

后端 未结 6 943
轻奢々
轻奢々 2020-12-14 18:23

I am using a UIPageViewController, and I need to get the scroll position of the ViewController as the users swipe so I can partially fade some assets while the

6条回答
  •  天涯浪人
    2020-12-14 18:35

    var pageViewController: PageViewController? {
        didSet {
            pageViewController?.dataSource = self
            pageViewController?.delegate = self
            scrollView?.delegate = self
        }
    }
    
    lazy var scrollView: UIScrollView? = {
        for subview in pageViewController?.view?.subviews ?? [] {
            if let scrollView = subview as? UIScrollView {
                return scrollView
            }
        }
        return nil
    }()
    
    extension BaseFeedViewController: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        let offset = scrollView.contentOffset.x
        let bounds = scrollView.bounds.width
        let page = CGFloat(self.currentPage)
        let count = CGFloat(viewControllers.count)
        let percentage = (offset - bounds + page * bounds) / (count * bounds - bounds)
    
        print(abs(percentage))
    }
    }
    

提交回复
热议问题