IOS: how to scroll between views using Swift

I'm new to Swift and iOS, and I'm looking at how to set up the main view, where I want to scroll right for the second view or leave the third view. When I am in the second or third view, I need to be able to return to the main view.

I found some ideas on how to implement a scroll view like this: https://medium.com/swift-programming/ios-swipe-view-with-swift-44fa83a2e078

But the β€œproblem” is that I want to start from the main view with the ability to scroll in both directions. (so the solution above is to start with the second view)

Does anyone know how to do this?

+7
ios swift view swipe viewcontroller
source share
2 answers

It is easy ...

stack overflow

enter image description here

+6
source share

This code works with Swift and 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] } } 
+3
source share

All Articles