如何从单元格触摸移动pageviewcontroller

时间:2017-05-22 07:39:35

标签: ios swift uipageviewcontroller

我有几个问题,请帮帮我。

如果我在didSelectItemAt委托协议上触摸CollectionViewCell,我想移动UIPageViewController的viewcontroller。 请帮我看看如何编写或修改下面的代码。

谢谢。

[查看页面]

class ListPageViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var openSlideMenuButton: UIBarButtonItem!

@IBOutlet weak var menuCollectionView: UICollectionView!

let menuCell = menuViewCell()
let menuItemNames = ["menu1", "menu2", "menu3", "menu4"]



@IBAction func moveHomeVC(_ sender: UIBarButtonItem) {
    self.navigationController?.popToRootViewController(animated: true)
}

override func viewDidLoad() {
    super.viewDidLoad()    

    ///////////////////////////////////////////////////////
    // Define NavigationBar
    self.navigationItem.hidesBackButton = true
    self.navigationController?.navigationBar.tintColor = UIColor.black
    self.navigationController?.navigationBar.barTintColor = UIColor.white
    var nevigatorHeight: CGFloat = 0
    nevigatorHeight = (self.navigationController?.navigationBar.frame.size.height)!

    //print("Navigation Height : \(nevigatorHeight)")

    ///////////////////////////////////////////////////////
    // Define Menu
    menuCollectionView.delegate = self
    menuCollectionView.dataSource = self

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menuItemNames.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "menuCellId", for: indexPath) as! menuViewCell

    cell.menuLabel.text = menuItemNames[indexPath.row]

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    //print("This page number is \(rootPage.viewControllerList.index(of: viewController)!).")
}   
}

[PageView]

import UIKit

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

lazy var viewControllerList: [UIViewController] = {
    let sb = UIStoryboard(name: "Main", bundle: nil)

    let vc1 = sb.instantiateViewController(withIdentifier: "RedVC")
    let vc2 = sb.instantiateViewController(withIdentifier: "BlueVC")
    let vc3 = sb.instantiateViewController(withIdentifier: "YellowVC")
    let vc4 = sb.instantiateViewController(withIdentifier: "GrayVC")

    return [vc1, vc2, vc3, vc4]
}()



override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self
    self.delegate = self

    if let firstViewController = viewControllerList.last {
        self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
    }

}

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

    let currentPage = viewControllerList.index(of: viewController)

    print("This page number is \(currentPage!) on your before gesture.")

    guard let vcIndex = currentPage else { return nil }

    let previousIndex = vcIndex - 1

    guard previousIndex >= 0 else { return nil }

    guard viewControllerList.count > previousIndex else { return nil }

    return viewControllerList[previousIndex]

}

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

    let currentPage = viewControllerList.index(of: viewController)


    print("This page number is \(currentPage!) on your after gesture. ")

    guard let vcIndex = currentPage else { return nil }

    let nextIndex = vcIndex + 1

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

    guard viewControllerList.count > nextIndex else { return nil }

    return viewControllerList[nextIndex]
}



}

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)中的UIPageviewController方法。

类似的问题: - How to properly advance a PageViewController in Swift