简体   繁体   English

UIScrollView具有多个页面可见或较小的页面大小

[英]UIScrollView with multiple pages visible or smaller page sizes

I'm trying to make a paging UIScrollView display multiple pages at the same time, in effect, making the page size smaller than the UIScrollview 's bounds. 我正在尝试使分页UIScrollView显示多个页面,实际上,使页面大小小于UIScrollview的界限。 I've been googling for hours, but nobody seems to have a good solution. 我一直在谷歌搜索几个小时,但似乎没有人有一个很好的解决方案。

I'm able to get the right effect visually by sizing the UIScrollview to the size I want one page to be, turning off subview clipping, and placing it inside a container that passes all of its taps to the UIScrollview . 我能够通过视觉大小得到正确的效果UIScrollview我想要一个页面是规模,关闭子视图的剪裁,并把它通过所有的水龙头到的容器内UIScrollview The problem with this is that with Y pages visible, it lets you scroll the last page all the way to the left, leaving Y-1 empty pages after the last page. 这样做的问题是,当Y页面可见时,它允许您将最后一页一直向左滚动,在最后一页之后留下Y-1空页面。 Anyone know a way around this, or another approach to the problem? 任何人都知道解决这个问题的方法,或另一种方法来解决问题?

I think you need something like the previews in the safari mobile browser. 我认为你需要像safari移动浏览器中的预览一样。 Here is a website with some sample code. 这是一个包含一些示例代码的网站。 Preview Sample Code 预览示例代码

For the right end, try reducing the width of the scroll view's contentSize property enough that the scroll view will stop paging before it gets to the last page. 对于右端,尝试减小滚动视图的contentSize属性的宽度,使滚动视图在到达最后一页之前停止分页。 For the left end, reduce the frame.origin.x property of each page by the same amount. 对于左端,将每个页面的frame.origin.x属性减少相同的量。 The first few pages will have a negative x position within the scroll view. 前几页在滚动视图中将具有负x位置。

Essentially, makeing the scroll view think that it's content is only pages 2 through second to last. 从本质上讲,使滚动视图认为它的内容只是第2页到第2页。

For example: 例如:

// 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, 0, pageWidth, pageHeight);
  UIView *pageView = [[[UIView alloc] initWithFrame:pageFrame] autorelease];

  // Initialize the page view for the current index here

  [scrollView addSubview:pageView];
}

Forgive any typos in the code. 原谅代码中的任何拼写错误。 I haven't tried this yet myself. 我自己还没试过这个。

Let me know if it works. 如果有效,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM