我使用以下代码在滚动
上添加更多图像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:pathright1];
[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];
但如果我有更多的图像,比如我有imagei应该添加更多代码,那么我可以使用循环(for?)来执行此功能吗?感谢
答案 0 :(得分:0)
尝试这样的事情:
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];