Progress of UIPageViewController

后端 未结 7 1111
礼貌的吻别
礼貌的吻别 2020-12-30 05:51

I would like to receive updates from the uipageviewcontroller during the page scrolling process. I want to know the transitionProgress in %. (This value should update when t

7条回答
  •  轮回少年
    2020-12-30 06:14

    KVO approach for Swift 4

    var myContext = 0
    
    override func viewDidLoad() {
        for view in self.view.subviews {
            if view is UIScrollView {
                view.addObserver(self, forKeyPath: "contentOffset", options: .new, context: &introPagingViewControllerContext)
            }
        }
    }
    
    // MARK: KVO
    
    override func observeValue(forKeyPath keyPath: String?,
                               of object: Any?,
                               change: [NSKeyValueChangeKey : Any]?,
                               context: UnsafeMutableRawPointer?)
    {
        guard let change = change else { return }
        if context != &myContext {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
            return
        }
    
        if keyPath == "contentOffset" {
            if let contentOffset = change[NSKeyValueChangeKey.newKey] as? CGPoint {
                let screenWidth = UIScreen.main.bounds.width
                let percent = abs((contentOffset.x - screenWidth) / screenWidth)
                print(percent)
            }
        }
    }
    

提交回复
热议问题