scrollView在视图之间滑动

时间:2014-09-02 11:27:01

标签: ios iphone uiscrollview swift

我正在尝试创建像snapChat这样的导航,您可以在不同视图之间滑动。我通过使用scrollView创建一个containsViewController创建了这个,然后有3个viewControllers将在scrollView中显示。这工作正常,但它将始终显示第一个viewController。我希望它首先呈现第二个viewController,以便您可以向右和向左滚动到其他viewControllers。我怎样才能获得这个。

containViewController

override func viewDidLoad() {
    super.viewDidLoad();

    // 1) Create the three views used in the swipe container view
    var BVc :BViewController =  BViewController(nibName: "BViewController", bundle: nil);
    var AVc :AViewController =  AViewController(nibName: "AViewController", bundle: nil);

    var CVc :CViewController =  CViewController(nibName: "CViewController", bundle: nil);


    // 2) Add in each view to the container view hierarchy
    //    Add them in opposite order since the view hieracrhy is a stack


    self.addChildViewController(BVc);
    self.scrollView!.addSubview(BVc.view);
    BVc.didMoveToParentViewController(self);

    self.addChildViewController(AVc);
    self.scrollView!.addSubview(AVc.view);
    AVc.didMoveToParentViewController(self);

    self.addChildViewController(CVc);
    self.scrollView!.addSubview(CVc.view);
    CVc.didMoveToParentViewController(self);


    // 3) Set up the frames of the view controllers to align
    //    with eachother inside the container view

    var BFrame :CGRect = BVc.view.frame;
    BFrame.origin.x = 2*BFrame.width;
    CVc.view.frame = BFrame;

    var adminFrame :CGRect = AVc.view.frame;
    adminFrame.origin.x = adminFrame.width;
    BVc.view.frame = adminFrame;




    // 4) Finally set the size of the scroll view that contains the frames
    var scrollWidth: CGFloat  = 3 * self.view.frame.width
    var scrollHeight: CGFloat  = self.view.frame.size.height
    self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight);
}

2 个答案:

答案 0 :(得分:1)

我建议使用UIPageViewController, 从UIPageViewControllerDataSource实现方法。

UIPageViewController允许您在ViewControllers之间滑动 使您能够配置ViewControllers的顺序和起始索引。

希望这有帮助!

答案 1 :(得分:0)

如果我理解正确,你在scrollView中有3个ContainerVV?

如果是这样,只需将scrollView的contentOffset值更改为第二个VC所在的位置。我对swift并不熟悉,但在Obj-C中,你做的是这样的事情:

self.scrollView!.contentOffset = CGPointMake(BVc.view.frame.origin.x, 0);