[英]Swift - infinite scroll between viewControllers
在我的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++;
}
覆蓋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和centralerIfNecessary方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.