水平的UIScrollView和数百个缩略图在iOS?

我需要创build一个水平的UIScrollView来保存数百个缩略图,就像缩略图一样。

例如,在一个屏幕上将显示10个缩略图,每个缩略图彼此水平相邻。

我的问题是,我不知道如何做一个水平的UIScrollView来同时显示多个缩略图?

示例照片如下。 看到屏幕的底部。

在这里输入图像说明

谢谢。

您可以将所有缩略图以编程方式添加到滚动视图中,并使用UIScrollView的setContentSize方法。 你必须在contentOffset中传递2个值。 1代表宽度,1代表高度。 请按照链接进一步了解这一点。 如果您需要进一步的帮助,请留下评论。

希望能帮助到你。

请考虑下面的例子。

- (void)setupHorizontalScrollView { scrollView.delegate = self; [self.scrollView setBackgroundColor:[UIColor blackColor]]; [scrollView setCanCancelContentTouches:NO]; scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; scrollView.clipsToBounds = NO; scrollView.scrollEnabled = YES; scrollView.pagingEnabled = YES; NSUInteger nimages = 0; NSInteger tot=0; CGFloat cx = 0; for (; ; nimages++) { NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", (nimages + 1)]; UIImage *image = [UIImage imageNamed:imageName]; if (tot==15) { break; } if (4==nimages) { nimages=0; } UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; CGRect rect = imageView.frame; rect.size.height = 40; rect.size.width = 40; rect.origin.x = cx; rect.origin.y = 0; imageView.frame = rect; [scrollView addSubview:imageView]; [imageView release]; cx += imageView.frame.size.width+5; tot++; } self.pageControl.numberOfPages = nimages; [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)]; } 

我build议你看看灵气

查看bjhomer的HSImageSidebarView项目。 它可以让你加载水平或垂直滚动​​视图和加载图像。 超级容易实现。

首先,在storyboard拖放滚动视图并使scrollview的出口名为scrollView 。 两个数组一个是mutable ,一个是immutable

 @property(nonatomic,strong)IBOutlet UIScrollView *scrollView; @property(nonatomic,strong)NSMutableArray *images; @property(nonatomic,strong)NSArray *imagesName; 

immutable变数组只存储我们想要在滚动视图上显示的图像。确保UIscrollview委托被定义。 在didload函数中的viewcontoller.m文件中,执行以下代码:

 imagesName = [[NSArray alloc]initWithObjects:@"centipede.jpg",@"ladybug.jpg",@"potatoBug.jpg",@"wolfSpider.jpg", @"ladybug.jpg",@"potatoBug.jpg",@"centipede.jpg",@"wolfSpider.jpg",nil]; // mutable array used to show the images on scrollview dynamic becaus after one // image when scroll other will come images = [[NSMutableArray alloc]init]; scrollView.delegate = self; scrollView.scrollEnabled = YES; int scrollWidth = 120; scrollView.contentSize = CGSizeMake(scrollWidth,80); int xOffset = 0; //the loop go till all images will load for(int index=0; index < [imagesName count]; index++) { UIImageView *img = [[UIImageView alloc] init]; // make the imageview object because in scrollview we need image img.frame = CGRectMake(5+xOffset, 0, 160, 110); // the offset represent the values, used so that topleft for each image will // change with(5+xOffset, 0)and the bottomright(160, 110) NSLog(@"image: %@",[imagesName objectAtIndex:index]); img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; // The image will put on the img object [images insertObject:img atIndex:index]; // Put the img object at the images array which is mutable array scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); //scroll view size show after 125 width the scroll view enabled [scrollView addSubview:[images objectAtIndex:index]]; // set images on scroll view xOffset += 170; } 

您可以计算滚动视图的内容大小宽度为宽度=每个图像的图像数量*大小。 然后将scrollview的contentSize设置为这个宽度和所需的高度(scrollView.contentSize = CGSizeMake(width, height))