2011-06-30 90 views
0

我用下面的代碼,以便在滾動如何輕鬆設置uiscroll上的uiimageview?

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應該添加更多的代碼,這樣我就可以使用循環(什麼?)做這個功能?感謝

回答

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]; 
+0

的NSString * pathright = [[一個NSBundle mainBundle] pathForResource:[的NSString stringWithFormat:@ 「圖像%d」,i + 1的] ofType:@ 「PNG」]; imageshow.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@「pathright%d」,i + 1]]; -----我認爲路徑不正確,所以無法運行 – matt

+0

對不起我的錯誤:)我編輯了我的代碼。 – DreamOfMirrors

+0

我看到你是一個新用戶,所以如果這個答案有用,你必須接受它爲「接受」。 – DreamOfMirrors

相關問題