滑动页面视图控制器时,段控制器的选定段应该更改

Selected segment of a segment controller should change when swiping a page view controller

我正在设计一个分段控件,我已经在其中实现了一个页面视图控制器以在视图之间滑动。而且我还实现了分段操作功能,以便在用户触摸任何特定分段时在视图之间切换。

但我现在面临的问题是,每当我 select 通过单击段来更改特定段时, selected 段会发生变化,但每当我在视图之间滑动时,我都无法更改select编辑索引。

所以我不能每次都获取加载的视图的索引吗?或者对我的问题有任何建议。

我实现了以下功能。

 func viewControllerAtIndex(index : Int) -> UIViewController/
    {

    }
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {

    }


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


    }

    @IBAction func sendsegmentcontrollerIndex(sender: AnyObject)

    {

  //codes for changing the present view controller on segment selection

    }

那么我现在必须做什么才能得到预期的结果? 我什至尝试向我的页面视图控制器添加滑动手势,但徒劳无功。

您应该覆盖此委托:pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)。也许你可以这样实现:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if pageViewController.viewControllers?.first?.isKindOfClass([yourClass]) {
        //self.changeIndex()
    }
}

在此委托中,您将检测页面移动的内容。您将设置您的段以与此 viewcontroller.

进行索引匹配
func pageViewController(pageViewController: UIPageViewController,
        didFinishAnimating finished: Bool,
        previousViewControllers: [UIViewController],
        transitionCompleted completed: Bool)
    {
        let pageContentViewController = pageViewController.viewControllers![0] as! ContentViewController
        let index = pageContentViewController.pageIndex
        print(index)
        seg.selectedSegmentIndex = index



    }