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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5973883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:53:02  来源:igfitidea点击:

Horizontal UIScrollView and hundreds of thumbnail images in iOS?

iphoneiosipad

提问by user403015

I need to create a horizontal UIScrollView which to hold hundreds of thumbnail images, just like a slide of thumbnails.

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

For example, there will be 10 thumbnails showing in a single screen, each of them are horizontally adjacent to each other.

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

My problem is that I don't know how to make a horizontal UIScrollView to hold the multiple thumbnails which showing at the same time ?

我的问题是我不知道如何制作水平 UIScrollView 来保存同时显示的多个缩略图?

A sample photo is as below. See the bottom part of the screen.

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

enter image description here

enter image description here

Thanks.

谢谢。

回答by Janak Nirmal

You can add all the thumbnails programatically to your scrollview and use the setContentSize method of UIScrollView. you have to pass 2 values in contentOffset. 1 for width and 1 for height. Please follow linkto explore more on this. If you need further help please leave a comment.

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

Hope it helps.

希望能帮助到你。

Please consider Following example.

请考虑以下示例。

- (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)];
}


回答by dnstevenson

Check out bjhomer's HSImageSidebarViewproject. It lets you load a scrollview horizontally or vertically and load in the images. Super easy to implement.

查看 bjhomer 的HSImageSidebarView项目。它允许您水平或垂直加载滚动视图并加载图像。超级容易实现。

回答by xonegirlz

I suggest you to look at nimbus

我建议你看nimbus

回答by Sarishti batta

First of all, at storyboarddrag and drop the scroll view and make the outlet of scrollviewnamed scrollView. Two array one is mutableand one is immutable.

首先,在storyboard拖放滚动视图并制作scrollviewnamed的出口scrollView。两个数组一个是mutable,一个是immutable

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

The immutablearray only store the images which we want to show on the scroll view.Make sure UIscrollviewdelegate is defined. In viewcontoller.mfile in didloadfunction do following code:

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

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;
}

回答by Sarishti batta

You can calculate content size width of the scrollview as width = number of images * size of each image. Then set contentSize of the scrollview to this width and the height that you want (scrollView.contentSize = CGSizeMake(width, height))

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