как легко установить uiimageview на uiscroll? - PullRequest
0 голосов
/ 30 июня 2011

я использую следующий код для добавления большего количества изображений на свитке

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, я должен добавить больше кода, поэтому я могу использовать цикл (для?)сделать эту функцию?спасибо

1 Ответ

0 голосов
/ 30 июня 2011

Попробуйте что-то вроде этого:

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];
...