【问题标题】:Swift - infinite scroll between viewControllersSwift - viewControllers 之间的无限滚动
【发布时间】:2015-07-25 13:17:45
【问题描述】:

在我的 ViewController 中,我有一个 UIScrollView,它包含两个 viewController(AviewController 和 BviewController)。 滚动工作正常但是,我想添加一个无限循环,这意味着当滚动到达最后一个视图( 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++;
    }

【问题讨论】:

    标签: ios iphone xcode swift uiscrollview


    【解决方案1】:

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

    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 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      相关资源
      最近更新 更多