PageViewController-禁用跳动问题

时间:2019-05-14 17:05:07

标签: ios swift

我有一个PageViewController,当前正在尝试禁用跳动。我已经找到了有关此问题的一些线索,但是给定的解决方案禁止弹跳到错误的一侧。

我有2个ViewController,分别是homeViewtimelineView-homeView最初显示且索引为0。显示此视图时,两侧的弹跳都被禁用,我无法滑动到下一个控制器。

有人看到这个问题吗?

我的代码:

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIScrollViewDelegate {

    lazy var viewControllerList:[UIViewController] = {

            let sb = UIStoryboard(name: "Main", bundle: nil)

            let vc1 = sb.instantiateViewController(withIdentifier: "homeView")
            let vc2 = sb.instantiateViewController(withIdentifier: "timelineView")

            return [vc1, vc2]

    }()

    var currentPage: Int = 0

    let defaults = UserDefaults.standard

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        NotificationCenter.default.addObserver(self, selector: #selector(disableSwipe(_:)), name: Notification.Name(rawValue: "disableSwipe"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(enableSwipe(_:)), name: Notification.Name(rawValue: "enableSwipe"), object: nil)

        for subview in self.view.subviews {
            if let scrollView = subview as? UIScrollView {
                scrollView.delegate = self
                break;
            }
        }

        if let secondViewController = viewControllerList.first as? HomeViewController {
            self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
            ...
        }
    }

    ...

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(currentPage, viewControllerList.count)
        if (currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
        } else if (currentPage == viewControllerList.count - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
        }
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        if (currentPage == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) {
            targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
        } else if (currentPage == viewControllerList.count - 1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
            targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
        }
    }

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

        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else { return nil }

        guard viewControllerList.count > previousIndex else { return nil }

        currentPage = previousIndex
        return viewControllerList[previousIndex]
    }

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

        guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

        let nextIndex = vcIndex + 1

        guard viewControllerList.count != nextIndex else { return nil }

        guard viewControllerList.count > nextIndex else { return nil }

        currentPage = nextIndex
        return viewControllerList[nextIndex]
    }
}

2 个答案:

答案 0 :(得分:0)

据我了解,您只是不希望页面控制器的UIScrollView在最后反弹,对吗?在这种情况下,您应该尝试设置scrollView.bounces = false。 (https://developer.apple.com/documentation/uikit/uiscrollview/1619420-bounces

答案 1 :(得分:0)

我发现了问题-它正在currentPage函数内部更改pageViewController的值。我必须将其移动到每个ViewController内部的viewDidAppear中。