9

I'm new to Swift and iOS development, and am looking how to set up a main view where I want swipe right for a second view or left for a third view. When I am in the second or the third view it should be possible to swipe back to the main view.

I found several ideas how to realize swipe view like this one: https://medium.com/swift-programming/ios-swipe-view-with-swift-44fa83a2e078

But the "problem" is, that I want to start on a main view with the possibility to swipe in both directions. (so with the solution above to start on the second view)

Does anyone know how to do this?

LilK3ks
  • 205
  • 2
  • 3
  • 14
  • Possible duplicate of [UIPageViewController and storyboard](https://stackoverflow.com/questions/18398796/uipageviewcontroller-and-storyboard) – Fattie May 28 '17 at 12:51

2 Answers2

7

It's this easy ...

stackoverflow.com/a/26024779/294884

enter image description here

Fattie
  • 27,874
  • 70
  • 431
  • 719
3

This code works together with Swift & Storyboarding (in the View Controller):

import UIKit

class ViewController : UIViewController, UIPageViewControllerDataSource {
    var myViewControllers = Array(count: 3, repeatedValue:UIViewController())

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        let pvc = segue.destinationViewController as UIPageViewController

        pvc.dataSource = self

       let storyboard = UIStoryboard(name: "Main", bundle: nil);

        var vc0 = storyboard.instantiateViewControllerWithIdentifier("shopID") as UIViewController
        var vc1 = storyboard.instantiateViewControllerWithIdentifier("startID") as UIViewController
        var vc2 = storyboard.instantiateViewControllerWithIdentifier("avatarID") as UIViewController

        self.myViewControllers = [vc0, vc1, vc2]

        pvc.setViewControllers([myViewControllers[1]], direction:.Forward, animated:false, completion:nil)

        println("Loaded")
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        var currentIndex =  find(self.myViewControllers, viewController)!+1
        if currentIndex >= self.myViewControllers.count {
            return nil
        }
        return self.myViewControllers[currentIndex]
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        var currentIndex =  find(self.myViewControllers, viewController)!-1
        if currentIndex < 0 {
            return nil
        }
        return self.myViewControllers[currentIndex]
    }

}
LilK3ks
  • 205
  • 2
  • 3
  • 14