Can i use the class below by other ViewController's class by clicking dots at Page Controller?

ios11 swift4 xcode9.1

24 观看

1回复

519 作者的声誉

enter image description hereThere are 4 views: DataViewController, GraphViewController and InfoViewController. PageViewController is made for switching between 3 others.

I've described everything to swipe my views by gestures. It works. However, how can I use it in another view by clicking dots at "Page Control" object from any of 3 views?

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
    }

}

Should i implement the object

Page Control

by: sender: PageViewController?

作者: Oleksandr 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

519 作者的声誉

First of all ad UIPageViewControllerDelegate and UIPageViewControllerData source to our class as follows:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

Then we add the following function, this is used to load the view controllers as you scroll through the different views.

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

Now above view did load add the following, the orderedViewControllers holds an array of View Controllers. If you want to add another View Controller to your page controller, this is the place to add it. We are adding in them by the storyboard identifiers sbBlue and sbRed we added earlier.

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

(sbBlue and sbRed are identifiers in storyboard for each View) Now add the following functions. These are used when your swiping through the page view controllers to load up the next view controller. This is configured so when you get to the last one, it will loop back to the start. If you swipe left on the first one it will load up the last one.

If you don’t want it to loop simply follow the instructions in the code to uncomment return nil and removing two lines of code. This is included in the code below.

// 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]
}

Now in viewDidLoad() add the following:

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)
}

**

Adding the page dot indicators

** To add the page dot indications add a pageControl as follows above the viewDidLoad()

var pageControl = UIPageControl()

Now add the following function. This will position the page control at the bottom of the screen. The current page indication will be black, and the rest of the indicators will be white. You can change these to suit the design of your app.

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)
}

Now in viewDidLoad() add these two lines:

  self.delegate = self
  configurePageControl()

And add the following function, this will make sure the page control indicator changes to the correct page as you scroll through.

// 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)!
}

Now run the app and you can see the page indicator as follows:

enter image description here

作者: Oleksandr 发布者: 2018 年 7 月 13 日
32x32