简体   繁体   English

2个视图之间的Swift和Page视图控制器

[英]Swift and Page view controller between 2 views

in my swift iOS app i would create a storyboard with two views. 在我快速的iOS应用程序中,我将创建一个具有两个视图的情节提要。 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 ] 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. 将UIPageViewController拖到情节提要中,并将其标记为初始视图控制器。
  2. Then create a new UIPageViewController subclass - say call it TutorialPageViewController and assign it to the object you just dragged into storyboard. 然后创建一个新的UIPageViewController子类-称其为TutorialPageViewController并将其分配给刚拖到情节UIPageViewController中的对象。
  3. Now drag in two new UIViewControllers into storyboard and create/assign a subclass for each, as you would do normally. 现在,像往常一样将两个新的UIViewController拖入情节提要中,并为每个创建/分配一个子类。
  4. Give each of these two view controllers a Storyboard ID eg RedViewController & GreenViewController . 给这两个视图控制器分别提供一个Storyboard ID例如RedViewControllerGreenViewController
  5. Now add this code in your TutorialPageViewController which creates and shows the correct view controller when you swipe: 现在,将此代码添加到您的TutorialPageViewController ,该代码可在您滑动时创建并显示正确的视图控制器:

TutorialPageViewController: 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/ 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: 从第2部分文章的第7点开始阅读,该文章显示了如何添加UIPageControl并更改滑动上的点:

https://spin.atomicobject.com/2016/02/11/move-uipageviewcontroller-dots/ 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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