我正在尝试使分页UIScrollView同时显示多个页面,实际上,使页面大小小于UIScrollview的界限.我一直在谷歌搜索几个小时,但似乎没有人有一个很好的解决方案.
通过将UIScrollview的大小调整到我想要的一个页面大小,关闭子视图剪辑,并将其放在一个容器中,将其所有的点击传递到UIScrollview,我能够在视觉上获得正确的效果.这样做的问题是,当Y页面可见时,它允许您将最后一页一直向左滚动,在最后一页之后留下Y-1空页面.任何人都知道解决这个问题的方法,或另一种方法来解决问题?
解决方法
对于右端,尝试减小滚动视图的contentSize属性的宽度,使滚动视图在到达最后一页之前停止分页.
对于左端,将每个页面的frame.origin.x属性减少相同的量.前几页在滚动视图中将具有负x位置.
对于左端,将每个页面的frame.origin.x属性减少相同的量.前几页在滚动视图中将具有负x位置.
从本质上讲,使滚动视图认为它的内容只是第2页到第2页.
例如:
// scrollView has been initialized and added to the view hierarchy according to the link in @richard's answer: // http://blog.proculo.de/archives/180-Paging-enabled-UIScrollView-With-Previews.html CGFloat pageNum=10; CGFloat pageWidth=scrollView.frame.size.width; CGFloat pageHeight=scrollView.frame.size.height; CGFloat visibleWidth=320; // Set scrollView contentSize and create pages: CGFloat leftShift=floor(((visibleWidth-pageWidth)/2)/pageWidth)*pageWidth; CGFloat contentSizeReduction=2*leftShift; scrollView.contentSize = CGSizeMake(pageNum*pageWidth-contentSizeReduction,pageHeight); for(int i=0; i<pageNum; i++) { CGRect pageFrame = CGRectMake(i*pageWidth-leftShift,pageWidth,pageHeight); UIView *pageView = [[[UIView alloc] initWithFrame:pageFrame] autorelease]; // Initialize the page view for the current index here [scrollView addSubview:pageView]; }
原谅代码中的任何拼写错误.我自己还没试过这个.
如果有效,请告诉我.