在UIScrollView中添加10个不同的UIImages

时间:2011-07-29 16:21:44

标签: iphone objective-c uiscrollview uiimageview uiimage

我正在尝试在UIImages下添加各种UIImageView,并允许它们使用UIScrollView滚动。我不确定如何在UIImageView下添加各种图像并让它们滚动。 下面是我的代码,它在UIImageView上添加了一个图像并使其可滚动。

- (void)viewDidLoad {

    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"ae.jpg"];
    imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = [[UIScreen mainScreen] bounds];
    imageView.contentMode = (UIViewContentModeScaleAspectFit);
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    imageView.backgroundColor = [UIColor clearColor];



    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.contentMode = (UIViewContentModeScaleAspectFit);

    scrollView.contentSize = CGSizeMake(image.size.width,960);
    scrollView.pagingEnabled = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.alwaysBounceVertical = NO;
    scrollView.alwaysBounceHorizontal = NO;
    scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    scrollView.maximumZoomScale = 2.5;
    scrollView.minimumZoomScale = 1;
    scrollView.clipsToBounds = YES;
   [scrollView addSubview:imageView];
   [image release];
    [imageView release];
   [self.view addSubview:scrollView];
}

1 个答案:

答案 0 :(得分:8)

这个想法基本上很简单。假设您要在UIScrollView中放置3个图像。 每张图片均为300x300。在这种情况下,您将拥有带框架的滚动视图:

scrollView.contentSize = CGSizeMake(image.size.width,900);

对于每个图像,你必须拥有适当框架的UIImageView:

imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:@"ProperName.png"];
...

(注意yOrigin(CGRectMake中的第二个值)) 然后和你一样:

[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];

当然,这是一个简短的代码,你会优化它;)