如何在ios中向scrollView添加多个图像

时间:2013-10-10 07:08:47

标签: ios

我有三个幻灯片的滚动视图,我想将图像添加到我的滚动视图。这是我的代码之后我需要添加什么

- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}

1 个答案:

答案 0 :(得分:0)

如果你想水平添加图像(比如3个图像),你必须添加宽度为3 * yourImageView.frame.size.width的滚动视图,然后你可以添加那些图像(x = 0,y = 0), (x = yourImageView.frame.size.width,y = 0)和(x = 2*yourImageView.frame.size.width,y = 0)

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = 3;
    imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)

    [scroll addSubview:imageView1];
    [scroll addSubview:imageView2];
    [scroll addSubview:imageView3];

启用水平滚动