gpt4 book ai didi

ios - 如何从单元格触摸移动 pageviewcontroller

转载 作者:行者123 更新时间:2023-11-28 08:09:20 24 4
gpt4 key购买 nike

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

如果我在 didSelectItemAt Delegate 协议(protocol)上触摸 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

最佳答案

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

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

关于ios - 如何从单元格触摸移动 pageviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44107252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com