我正在寻找一种实现以下内容的方法:
为了了解这是如何设置的,下面是我当前的实现:
// Initialise components:
mainScreen = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = mainScreen.size.height-20;
// Scroll View Controller
_scrollControl = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, screenHeight)];
_scrollControl.contentSize = CGSizeMake(320, 2*screenHeight); // Twice as big as the screen size for both views to fit
_scrollControl.backgroundColor = [UIColor clearColor];
_scrollControl.delegate = self;
// Top View
_topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, screenHeight)];
_topView.backgroundColor = [UIColor redColor];
[_scrollControl addSubview:_topView];
// Bottom View
_bottomView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, screenHeight)];
_bottomView.backgroundColor = [UIColor yellowColor];
_bottomView.contentSize = CGSizeMake(320, 2*screenHeight);
_bottomView.delegate = self;
UILabel *imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 700)];
imageLabel.backgroundColor = [UIColor greenColor];
[_bottomView addSubview:imageLabel];
[_scrollControl addSubview:_bottomView];
// Add to main view
[self.view addSubview:_scrollControl];我试图使用委托方法来达到预期的效果,但是在“主”scrollView切换到底部scrollView之前,我似乎无法阻止它滚动。
发布于 2013-06-07 09:01:07
苹果公司免费提供这个功能,所以好消息是你不需要在这里直接编码任何东西(除非你想要一些古怪的东西)。这种效果是通过分页实现的。
scrollView.pagingEnabled = YES;在产生任何实际效果之前,您需要在主滚动视图中嵌入您的子视图( UIView和UIScrollView)。它们中的每一个都应该是相同的大小,或者一个页面的大小。假设主滚动视图是100,100点,我们可以这样设置它:
CGRect pageRect = CGRectMake(0, 0, 100, 100);
NSInteger pageCount = 2;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:pageRect];
scrollView.pagingEnabled = YES;
UIView *page1View = [[UIView alloc] initWithFrame:pageRect];
page1View.backgroundColor = [UIColor redColor];
[scrollView addSubview:page1View];
pageRect.origin.x += pageRect.size.width;
UIView *page2View = [[UIView alloc] initWithFrame:pageRect];
page2View.backgroundColor = [UIColor blueColor];
[scrollView addSubview:page2View];
scrollView.contentSize = CGSizeMake(pageRect.size.width * pageCount,
pageRect.size.height);这应该能给你一些基本的东西。如您所见,在本例中,我们将子视图一个接一个地放置在水平位置。我们将内容大小设置为覆盖子视图和启用分页。UIScrollView应该负责其余的工作。
一个很好的地方是WWDC的会话视图,特别是来自WWDC 2010 : session 104 -设计带有滚动视图的应用程序。这有很多关于如何设置滚动视图并真正充分利用它们的信息。
希望这能有所帮助!
发布于 2014-10-01 13:27:12
因此,如果您想要使用swift检测到它,请使用以下命令:
override func scrollViewDidScroll(scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//reach bottom
}
if (scrollView.contentOffset.y < 0){
//reach top
}
if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){
//not top and not bottom
}
}https://stackoverflow.com/questions/16979865
复制相似问题