我是Swift和iOS开发的新手,我正在寻找如何设置主视图,我想要向右滑动第二个视图或向左移动第三个视图。 当我在第二个或第三个视图中时,应该可以滑回主视图。
我找到了几个想法如何实现这样的滑动视图: https://medium.com/swift-programming/ios-swipe-view-with-swift-44fa83a2e078
但是“问题”是,我想从主视图开始,可以向两个方向滑动。 (所以上面的解决方案从第二个视图开始)
有谁知道怎么做?
答案 0 :(得分:7)
答案 1 :(得分:3)
此代码与Swift& amp;故事板(在视图控制器中):
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]
}
}