2017-05-22 40 views
1

我有幾個問題,請幫助我。如何從單元格觸摸移動pageviewcontroller

我想移動UIPageViewController的viewcontroller,如果我觸摸了didSelectItemAt委託協議上的CollectionViewCell。 請幫助我如何編寫或修改下面的代碼。

謝謝。

[查看頁]

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

回答