gpt4 book ai didi

swift 4 UIPageViewControler

转载 作者:行者123 更新时间:2023-11-30 10:50:14 24 4
gpt4 key购买 nike

我有三个 ViewControllera, b , c 我想将第一个 viewController 初始化为 (b),然后向左滑动到 viewController(a),向右滑动到 viewController(c)

如何通过UIPageViewController制作

最佳答案

您需要在 Main.storyboard 中拥有 3 个带有标识符 Page1、Page2 和 Page3 的 ViewController。

import UIKit

class PageViewController: UIPageViewController
{
fileprivate lazy var pages: [UIViewController] = {
return [
self.getViewController(withIdentifier: "Page1"),
self.getViewController(withIdentifier: "Page2"),
self.getViewController(withIdentifier: "Page3")
]
}()

fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
}

override func viewDidLoad()
{
super.viewDidLoad()
self.dataSource = self
self.delegate = self

if let firstVC = pages.first
{
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
}
}

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

guard let viewControllerIndex = pages.index(of: viewController) else { return nil }

let previousIndex = viewControllerIndex - 1

guard previousIndex >= 0 else { return pages.last }

guard pages.count > previousIndex else { return nil }

return pages[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
guard let viewControllerIndex = pages.index(of: viewController) else { return nil }

let nextIndex = viewControllerIndex + 1

guard nextIndex < pages.count else { return pages.first }

guard pages.count > nextIndex else { return nil }

return pages[nextIndex]
}
}

extension PageViewController: UIPageViewControllerDelegate { }

关于swift 4 UIPageViewControler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745762/

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