Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 10 different UIImages in UIScrollView

I am trying to add various UIImages under UIImageView and allow them to scroll with UIScrollView. I am not sure how to add various images under UIImageView and let them scroll. Below is my code which adds an image on UIImageView and make it scrollable.

- (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];
}
like image 758
lifemoveson Avatar asked Feb 24 '23 10:02

lifemoveson


1 Answers

The idea is basically simple. Let's assume you want to place 3 images in UIScrollView. Each of images is 300x300. In this case you'll have scroll view with frame:

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

For every image you must have it's UIImageView with proper frame:

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

(pay attention to the yOrigin (2nd value in CGRectMake)) and then as you did:

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

Of course, it's a brief code, you'll optimize it ;)

like image 170
makaron Avatar answered Mar 07 '23 17:03

makaron