简体   繁体   English

Swift-ViewControllers之间的无限滚动

[英]Swift - infinite scroll between viewControllers

In my ViewController I have a UIScrollView that contains two viewControllers (AviewController and BviewController). 在我的ViewController中,我有一个UIScrollView,其中包含两个viewController(AviewController和BviewController)。 The scrolling works fine however, I would like to add an infinite loop to it means that when the scroll reaches the last view ( BviewController ) AviewController should be next and vice-versa. 滚动工作正常,但是,我想向其中添加一个无限循环,这意味着当滚动到达最后一个视图(BviewController)时,AviewController应该位于下一个视图,反之亦然。

Cannot find the way to implement this. 找不到实现此方法的方法。 Any Ideas? 有任何想法吗?

So far I have in viewDidLoad: 到目前为止,我在viewDidLoad中:

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

    let AviewController = storyboard.instantiateViewControllerWithIdentifier("A_id") as! AviewController;
    let BviewController = storyboard.instantiateViewControllerWithIdentifier("B_id") as! BviewController;

    scrollView!.contentSize = CGSizeMake(2*CGRectGetWidth(AviewController.view.frame), CGRectGetHeight(view.frame));

    let viewControllers = [AviewController, BviewController]

    var idx:Int = 0;

    for viewController in viewControllers {
        // index is the index within the array
        // participant is the real object contained in the array
        addChildViewController(viewController);
        let originX:CGFloat = CGFloat(idx) * CGRectGetWidth(scrollView!.frame);
        viewController.view.frame = CGRectMake(originX, 0, viewController.view.frame.size.width, viewController.view.frame.size.height);
        scrollView!.addSubview(viewController.view)
        viewController.didMoveToParentViewController(self)
        idx++;
    }

One way to override UIScrollView, pad the content size to much larger than your content, then override layoutSubviews to re-position views as you scroll. 覆盖UIScrollView的一种方法是,将内容大小填充到比您的内容大得多的位置,然后覆盖layoutSubviews以在滚动时重新放置视图。

An example provided by Apple is here: https://developer.apple.com/library/ios/samplecode/StreetScroller/Introduction/Intro.html Apple提供的示例在这里: https : //developer.apple.com/library/ios/samplecode/StreetScroller/Introduction/Intro.html

In particular look at the InfiniteScrollView file https://developer.apple.com/library/ios/samplecode/StreetScroller/Listings/StreetScroller_InfiniteScrollView_m.html 特别要看一下InfiniteScrollView文件https://developer.apple.com/library/ios/samplecode/StreetScroller/Listings/StreetScroller_InfiniteScrollView_m.html

And the layoutSubviews and recenterIfNecessary methods. 以及layoutSubviews和centralerIfNecessary方法。

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

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