快速滚动UIPageViewController阻止ViewController更新

时间:2018-08-23 17:15:50

标签: ios swift uipageviewcontroller

我有一个UIPageviewcontroller,里面有两个控制器。当您滑动到下一个时,我使用viewController参数设置适当的委托。但是我体验到,如果您滑动得太快,则函数viewControllerAfter无法正确更新viewController。最初的滑动应将ViewController的索引从0更新为1,但如果滑动速度过快,则不会这样做。

import UIKit

class WizardPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVc(viewController: "intro"),
                self.newVc(viewController: "welcome")]
    }()

    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        self.delegate = self
        configurePageControl()

        // This sets up the first view that will show up on our page control
        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
    }

    func newVc(viewController: String) -> UIViewController {
        return UIStoryboard(name: "Wizard", bundle: nil).instantiateViewController(withIdentifier: viewController)
    }

    func configurePageControl() {
        // The total number of pages that are available is based on how many available colors we have.
        pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 225,width: UIScreen.main.bounds.width,height: 50))
        self.pageControl.numberOfPages = orderedViewControllers.count
        self.pageControl.currentPage = 0
        pageControl.isEnabled = false
        //self.pageControl.tintColor = UIColor.black
        self.pageControl.pageIndicatorTintColor = UIColor.gray
        self.pageControl.currentPageIndicatorTintColor = UIColor(red:0.647, green:0.192, blue:0.216, alpha:1.00)
        self.view.addSubview(pageControl)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        print(orderedViewControllers.index(of: viewController))
        let previousIndex = viewControllerIndex - 1

        // User is on the first view controller and swiped left to loop to
        // the last view controller.
        guard previousIndex >= 0 else {
            return nil
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            // return nil
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }

        return orderedViewControllers[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        print(orderedViewControllers.index(of: viewController)) // Returns 0 is I swipe too fast, otherwise 1
        if let vc = orderedViewControllers[viewControllerIndex] as? WelcomeViewController {
            vc.delegate = self
        }

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count

        // User is on the last view controller and swiped right to loop to
        // the first view controller.
        guard orderedViewControllersCount != nextIndex else {
            return nil
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            // return nil
        }

        guard orderedViewControllersCount > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]
    }

}

2 个答案:

答案 0 :(得分:2)

我遇到了完全相同的问题。基本上,它归结为UIPageViewController(确切地说是_UIQueuingScrollView),无法正确更新其视图层次结构。

我注意到UIPageViewController足够聪明,即使页面保持不变也不会添加/删除内容视图(即使它在视图层次结构中的位置不正确,它也会设法解决该问题,因此看起来不错用户)。这就是为什么我添加了PageTrackingView来观察正在(或没有)添加到UIPageViewController视图层次结构中的内容视图的原因。通过跟踪更改是否发生,我可以通过翻转来计算当前索引。因此,目前的解决方法仅适用于 2页

class PageTrackingView: UIView {

    var pageIndexable: PageIndexable?

    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        if var pageIndexable = pageIndexable {
            let newIndex = (pageIndexable.internalIndex == 0) ? 1 : 0
            pageIndexable.internalIndex = newIndex
        }
        //Reset pageIndexable so that the index flip is fired only once
        pageIndexable = nil
    }
}

protocol PageIndexable {
    var internalIndex: Int { get set }
}

class PageViewController: UIPageViewController,
    UIPageViewControllerDataSource,
    UIPageViewControllerDelegate,
    PageIndexable {

    private var supportedViewControllers = [UIViewController]()

    internal var internalIndex: Int = 0 {
    didSet {
        if supportedViewControllers.indices.contains(internalIndex) {
            //Do something with the actual internalIndex value
        } else {
            assertionFailure()
        }
    }

    init(transitionStyle style: UIPageViewControllerTransitionStyle = .scroll,
        navigationOrientation: UIPageViewControllerNavigationOrientation = .horizontal,
        options: [String: Any]? = nil,
        viewControllers: [T]) {
        supportedViewControllers = viewControllers

        if !supportedViewControllers.isEmpty {
            let viewController = supportedViewControllers[0]
            let trackingView = PageTrackingView(frame: viewController.view.frame)
            viewController.view.frame = viewController.view.bounds
            trackingView.addSubview(viewController.view)
            viewController.view = trackingView
        }
        super.init(transitionStyle: style,
            navigationOrientation: navigationOrientation,
            options: options)
    }

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

        if !supportedViewControllers.isEmpty, let trackingView = supportedViewControllers[0].view as? PageTrackingView {
            trackingView.pageIndexable = self
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        datasource = self
    }
}

答案 1 :(得分:0)

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let index = arrayVCs.index(of: viewController) else {
        return nil
    }

    if index == 0 {
        return nil
    }

    let prevIndex = abs((index - 1) % arrayVCs.count)
    return arrayVCs[prevIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    guard let index = arrayVCs.index(of: viewController) else {
        return nil
    }

    if index == arrayVCs.count - 1 {
        return nil
    }

    let nextIndex = abs((index + 1) % arrayVCs.count)
    return arrayVCs[nextIndex]

}

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

        if let viewController = pageViewController.viewControllers?[0] {
            guard let index = arrayVCs.index(of: viewController) else {
                return
            }
            self.segment.selectedSegmentIndex = index
        }
    }

尝试一下。对我来说有用。