0

在此处输入图像描述有 4 个视图:DataViewController、GraphViewController 和 InfoViewController。PageViewController 用于在其他 3 个之间切换。

我已经描述了通过手势滑动我的视图的所有内容。有用。但是,如何通过从 3 个视图中的任何一个单击“页面控制”对象上的点来在另一个视图中使用它?

import UIKit

class PageViewController: UIPageViewController
{
    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.ListedViewController(name: "Data"),
                self.ListedViewController(name: "Graph"),
                self.ListedViewController(name: "Info")]
    }()

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

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

    private func ListedViewController(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewController(withIdentifier: "\(name)ViewController")
    }
}

// MARK: UIPageViewControllerDataSource

extension PageViewController: UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerBefore viewController: UIViewController) -> UIViewController?
        {
            guard let viewControllerIndex = orderedViewControllers.index(of: viewController)
                else
            {
                return nil
            }

            let previousIndex = viewControllerIndex - 1

            // User is on the first view controller and swiped left to loop to
            // the last view controller.
            guard previousIndex >= 0
            else
            {
                return orderedViewControllers.last
            }

            guard orderedViewControllers.count > previousIndex
            else
            {
                return nil
            }

            return orderedViewControllers[previousIndex]
        }

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

            let nextIndex = viewControllerIndex + 1
            let orderedViewControllersCount = orderedViewControllers.count

            // User is on the last view controller and swiped right to loop to
            // the first view controller.
            guard orderedViewControllersCount != nextIndex else
            {
                return orderedViewControllers.first
            }

            guard orderedViewControllersCount > nextIndex else
            {
                return nil
            }

            return orderedViewControllers[nextIndex]
        }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
            let firstViewControllerIndex = orderedViewControllers.index(of: firstViewController) else {
                return 0
        }

        return firstViewControllerIndex
    }

}

我应该实现对象吗

页面控制

通过:sender: PageViewController

4

1 回答 1

0

首先将 UIPageViewControllerDelegate 和 UIPageViewControllerData 源广告到我们的类中如下:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

然后我们添加以下函数,用于在滚动浏览不同视图时加载视图控制器。

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

现在上面的视图确实加载了以下内容,orderedViewControllers 包含一个视图控制器数组。如果您想将另一个视图控制器添加到您的页面控制器,这是添加它的地方。我们通过之前添加的故事板标识符 sbBlue 和 sbRed 添加它们。

lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVc(viewController: "sbBlue"),
                self.newVc(viewController: "sbRed")]
    }()

(sbBlue 和 sbRed 是情节提要中每个视图的标识符)现在添加以下函数。当您在页面视图控制器中滑动以加载下一个视图控制器时使用这些。这是配置的,因此当您到达最后一个时,它将循环回到起点。如果您在第一个上向左滑动,它将加载最后一个。

如果您不希望它循环,只需按照代码中的说明取消注释 return nil 并删除两行代码。这包含在下面的代码中。

// MARK: Data source functions.
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    // User is on the first view controller and swiped left to loop to
    // the last view controller.
    guard previousIndex >= 0 else {
        return orderedViewControllers.last
        // Uncommment the line below, remove the line above if you don't want the page control to loop.
        // return nil
    }

    guard orderedViewControllers.count > previousIndex else {
        return nil
    }

    return orderedViewControllers[previousIndex]
}

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

    let nextIndex = viewControllerIndex + 1
    let orderedViewControllersCount = orderedViewControllers.count

    // User is on the last view controller and swiped right to loop to
    // the first view controller.
    guard orderedViewControllersCount != nextIndex else {
        return orderedViewControllers.first
        // Uncommment the line below, remove the line above if you don't want the page control to loop.
        // return nil
    }

    guard orderedViewControllersCount > nextIndex else {
        return nil
    }

    return orderedViewControllers[nextIndex]
}

现在在 viewDidLoad() 中添加以下内容:

self.dataSource = self

// This sets up the first view that will show up on our page control
if let firstViewController = orderedViewControllers.first {
    setViewControllers([firstViewController],
                       direction: .forward,
                       animated: true,
                       completion: nil)
}

**

添加页点指示器

** 要添加页面点指示,请在 viewDidLoad() 上方添加一个 pageControl,如下所示

var pageControl = UIPageControl()

现在添加以下函数。这会将页面控件定位在屏幕底部。当前页面指示为黑色,其余指示为白色。您可以更改这些以适合您的应用程序的设计。

func configurePageControl() {
    // The total number of pages that are available is based on how many available colors we have.
    pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
    self.pageControl.numberOfPages = orderedViewControllers.count
    self.pageControl.currentPage = 0
    self.pageControl.tintColor = UIColor.black
    self.pageControl.pageIndicatorTintColor = UIColor.white
    self.pageControl.currentPageIndicatorTintColor = UIColor.black
    self.view.addSubview(pageControl)
}

现在在 viewDidLoad() 添加这两行:

  self.delegate = self
  configurePageControl()

并添加以下功能,这将确保页面控制指示器在您滚动时更改为正确的页面。

// MARK: Delegate functions
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    let pageContentViewController = pageViewController.viewControllers![0]
    self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}

现在运行应用程序,您可以看到页面指示器如下:

在此处输入图像描述

于 2018-07-13T10:52:46.797 回答