ios - Swift - viewController 之间无限滚动

标签 ios iphone xcode swift uiscrollview

在我的 ViewController 中,我有一个 UIScrollView,其中包含两个 viewController(AviewController 和 BviewController)。 滚动工作正常,但是,我想添加一个无限循环,这意味着当滚动到达最后一个 View ( BviewController )时,AviewController 应该是下一个,反之亦然。

找不到实现此目的的方法。有什么想法吗?

到目前为止我已经在 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++;
    }

最佳答案

覆盖 UIScrollView 的一种方法,将内容大小填充到比您的内容大得多,然后覆盖 layoutSubviews 以在滚动时重新定位 View 。

Apple 提供的示例如下:https://developer.apple.com/library/ios/samplecode/StreetScroller/Introduction/Intro.html

特别查看 InfiniteScrollView 文件 https://developer.apple.com/library/ios/samplecode/StreetScroller/Listings/StreetScroller_InfiniteScrollView_m.html

以及layoutSubviews和recenterIfNecessary方法。

关于ios - Swift - viewController 之间无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31626860/

相关文章:

iphone - 使用加速度计执行倾斜操作

c++ - Xcode4 和 OpenGL - 程序收到信号 : EXC_BAD_ACCESS

ios - 自定义单元格未显示在 TableView -Xcode 中

xcode - install_name_tool报告格式错误的对象

ios - SKScene : find max position of SKSpriteNode

ios - RxSwift - 误解了一些概念

objective-c - 使用 initWithObjects : causes crash, 但 initWithCapacity: 后跟 addObject: 不会

iphone - 核心数据 : NSCocoaErrorDomain=134040 Only Occurs On Real Phone, 不是模拟器

iphone - 如何在 Facebook 上发布带有 url 的消息?

iphone - 如何在启动时使用黑色半透明状态栏 - info.plist 键/值似乎不起作用