代码之家  ›  专栏  ›  技术社区  ›  aaronium112

UIPageViewController添加为子视图时无法识别滑动

  •  0
  • aaronium112  · 技术社区  · 6 年前

    更重要的是,委托或数据源方法都没有被调用。

    setupPageView是从拥有pageViewContoller嵌入的视图的viewController的viewDidLoad调用的。

    有什么东西我在看吗?

    func setupPageView() {
        pageView = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [.interPageSpacing : 9])
        pageView.dataSource = self
        pageView.delegate = self
        pageView.setViewControllers([graphViews[0]], direction: .forward, animated: true, completion: nil)
    
        self.addChild(pageView)
        let bounds = pageViewContainer.bounds
        pageView.view.frame = bounds
        pageViewContainer.addSubview(pageView.view)
        pageView.didMove(toParent: self)
    }
    

    使用下面的代码手动更改页面可以正常工作:

    @IBAction func segmentedControlChanged(_ sender: UISegmentedControl) {
        var direction: UIPageViewController.NavigationDirection = .forward
        let newIndex = sender.selectedSegmentIndex
        if newIndex == index {
            return
        }
        if newIndex < index {
            direction = .reverse
        }
        index = newIndex
        pageView.setViewControllers([graphViews[index]], direction: direction, animated: true, completion: nil)
    }
    

    扩展图PageView:UIPageViewControllerDelegate{

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    
        if completed {
            //update index
            guard let currentVC = pageViewController.viewControllers?[0] else { fatalError("No Current VC")}
            guard let currentIndex = graphViews.index(of: currentVC) else { fatalError("No VC found") }
            self.index = currentIndex
        }
    }
    

    }

    扩展图PageView:UIPageViewControllerDataSource{

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        //
        if index == 0 { //@ begining of stack
            return nil
        } else {
            return graphViews[index - 1]
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        //
        if index == graphViews.count { //@ end of stack
            return nil
        } else {
            return graphViews[index + 1]
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Andras M.    6 年前

    您可以尝试通过以下行允许交互

    pageView.isUserInteractionEnabled = true
    

    这有助于将视图封装到其他视图中,但需要捕获触摸

        2
  •  0
  •   Shehata Gamal    6 年前

    你应该改变里面的索引 viewControllerBefore &安培; viewControllerAfter 为了反映当前索引,因此当您滚动时,分别获得下一个/上一个索引,请查看以下内容 pager