iOS 中的水平 UIScrollView 和数百个缩略图? [英] Horizontal UIScrollView and hundreds of thumbnail images in iOS?

查看:18
本文介绍了iOS 中的水平 UIScrollView 和数百个缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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 ?

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

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

谢谢.

推荐答案

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

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 link to explore more on this. If you need further help please leave a comment.

希望有帮助.

请考虑以下示例.

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

这篇关于iOS 中的水平 UIScrollView 和数百个缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆