如何在水平UIPageViewController中实现垂直UIPageViewController

时间:2015-11-17 20:50:04

标签: ios xcode swift uipageviewcontroller

enter image description here

我想调整我的UIPageViewController以便像在图片中一样工作,目前 1b 能够 2a 等。我需要做什么锁定图像中的方向?我忘了什么,或者我使用了错误的逻辑。任何小费都会有所帮助!

注意垂直UIPageViewController位于水平UIPageViewController

水平UIPageViewController代码

class MyPageViewController: UIPageViewController , UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pages = [UIViewController]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    let page1: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page1")
    let page2: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page2")
    let page3: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page3")


    pages.append(page1)
    pages.append(page2)
    pages.append(page3)


    setViewControllers([page2], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}

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


    let currentIndex = pages.indexOf(viewController)!


    let previousIndex = abs((currentIndex - 1) % pages.count)


    if (previousIndex > 0)
    {
        return nil
    }



    return pages[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let currentIndex = pages.indexOf(viewController)!
    let nextIndex = abs((currentIndex + 1) % pages.count)


    if (nextIndex < 1)

    {
        return nil
    }

    return pages[nextIndex]

}

垂直UIPageViewController代码

class upAndDownViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pages = [UIViewController]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    let midPage: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("midPage")
    let topPage: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("topPage")



    pages.append(midPage)
    pages.append(topPage)



    setViewControllers([midPage], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)




}

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


    let currentIndex = pages.indexOf(viewController)!
    let nextIndex = abs((currentIndex + 1) % pages.count)


    if (nextIndex < 1)
    {
        return nil
    }

    return pages[nextIndex]
}

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

    let currentIndex = pages.indexOf(viewController)!


    let previousIndex = abs((currentIndex - 1) % pages.count)


    if (previousIndex > 0)
    {
        return nil
    }



    return pages[previousIndex]


}

1 个答案:

答案 0 :(得分:1)

我将水平UIPageViewController放在垂直UIPageViewController内。修复它但由于某些原因,在 TOP 垂直UIPageViewController内部打开的其他ViewControllers能够向下翻页,我会更多地了解它并可能修复它。