Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

iphone - Horizontal UIScrollView and hundreds of thumbnail images in iOS?

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

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

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.

enter image description here

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...