简体   繁体   中英

Swift and Page view controller between 2 views

in my swift iOS app i would create a storyboard with two views. I want use the page view controller element, so, with one left / right swipe, the user can change the actual view. How can I do this?

IMPORTANT! I have searched several time for tutorials in the web. I have only found schema which allow to change images in the same image view. I don't want to change the image in the same image in the same view; i want change the entire view. I attach a image, to better explain.

Thank in advance ![模式 ] 1

This explains how to create a paged view controller using storyboard:

  1. Drag a UIPageViewController into storyboard and mark it as the initial view controller.
  2. Then create a new UIPageViewController subclass - say call it TutorialPageViewController and assign it to the object you just dragged into storyboard.
  3. Now drag in two new UIViewControllers into storyboard and create/assign a subclass for each, as you would do normally.
  4. Give each of these two view controllers a Storyboard ID eg RedViewController & GreenViewController .
  5. Now add this code in your TutorialPageViewController which creates and shows the correct view controller when you swipe:

TutorialPageViewController:

class TutorialPageViewController: UIPageViewController, UIPageViewControllerDataSource {
private(set) lazy var orderedViewControllers: [UIViewController] = {
    return [self.newColoredViewController("RedViewController"),
    self.newColoredViewController("GreenViewController")]
}()

private func newColoredViewController(color: String) -> UIViewController {

return UIStoryboard(name: "Main", bundle: nil) .
    instantiateViewControllerWithIdentifier("\(color)ViewController")
}

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = self

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

func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return nil
    }

    guard orderedViewControllers.count > previousIndex else {
        return nil
    }

    return orderedViewControllers[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
        return nil
    }

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

    guard orderedViewControllersCount != nextIndex else {
        return nil
    }

    guard orderedViewControllersCount > nextIndex else {
        return nil
    }

    return orderedViewControllers[nextIndex]
}
}

See the article for more details and a tutorial on this.

https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/

Update: Page Dots

Read from point 7 on this part 2 article that shows how to add a UIPageControl and change the dots on swipe:

https://spin.atomicobject.com/2016/02/11/move-uipageviewcontroller-dots/

@IBOutlet weak var pageControl: UIPageControl!

func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
    didUpdatePageCount count: Int) {
    pageControl.numberOfPages = count
}

func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
    didUpdatePageIndex index: Int) {
    pageControl.currentPage = index
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM