在UIPageViewController中维护scrollview的contentoffset

时间:2018-07-14 08:19:04

标签: ios swift animation uiscrollview uipageviewcontroller

我的目标是确定用户在UIPageViewController中相对于特定ViewController的滚动距离。我使用以下方法跟踪当前的视图控制器索引:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    guard completed else { return }

    // switch on viewcontroller
    guard let vc = pageViewController.viewControllers?.first else { return }
    let index: Int
    switch vc {
    case is ViewController0:
        index = 0
    case is ViewController1:
        index = 1
    case is ViewController2:
        index = 2
    default:
        index = 2
    }
    currentIndex = index
}

我还可以通过以下方法跟踪滚动量:

extension PageViewController: UIScrollViewDelegate {

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
     let point = scrollView.contentOffset
     var percentComplete = fabs(point.x -view.frame.size.width)/view.frame.size.width

    //do something based on percent complete

        }
}

当您连续滚动多页时会发生此问题,因此currentIndex永远不会更新,而视图控制器的contentOffset会更新。

0 个答案:

没有答案