how set the uiimageview on uiscroll easily?
i use the following code for adding more images on scroll
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:@"images0" ofType:@"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(0, 0, 890, 430)];
UIImageView* imageshow1=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright1 = [[NSBundle mainBundle] pathForResource:@"images1" ofType:@"png"];
imageshow1.image = [UIImage imageWithContentsOfFile:path开发者_开发技巧right1];
[imageshow1 setFrame:CGRectMake(890, 0, 890, 430)];
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*2,430)];
scrollViewright.pagingEnabled = YES;
[scrollViewright addSubview:imageshow];
[scrollViewright addSubview:imageshow1];
[self.view addSubview:scrollViewright];
but if i have more images,such as i have imagei should add more code ,so can i use loop (for?) to do this function? thanks
Try something like this:
NSUInteger tot = 5;
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*tot,430)];
scrollViewright.pagingEnabled = YES;
for (NSUInteger i = 0; i < tot; ++i) {
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"images%d", i + 1] ofType:@"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(i * 890, 0, 890 * (i + 1), 430)];
[scrollViewright addSubview:imageshow];
[imageshow release];
}
[self.view addSubview:scrollViewright];
精彩评论