darkman
darkman

Reputation: 1003

Zoom in Scroll view with UIImage

Im trying to zoom in a scroll view with multiple images, the scroll view only scrolls vertical, i tried to implement many ways, in many examples but none seens to work. Heres the code im using to create the images and the scroll view:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    for (int i = 0; i < 6; i++)
    {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"amap1%i.png",i+1]];
        // create imageView
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height*i, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        // set scale to fill
        _imageView.contentMode = UIViewContentModeScaleToFill;
        // set image
        [_imageView setImage:image];
        // add to scrollView
        [self.scrollView addSubview:_imageView];
    }

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height * 6);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
}

Upvotes: 0

Views: 2010

Answers (2)

Dhaval
Dhaval

Reputation: 506

Looks like you'll need to do a UIScrollView (to contain all the images - horizontal scrolling) with a UIScrollView for each image (to zoom in).

There's a WWDC video which talks about how to do this (WWDC 2010 - Session 104; link to iTunes)

Upvotes: 1

Robin van Dijke
Robin van Dijke

Reputation: 752

If you want to implement zoom in an UIScrollView you have to do the following things

  1. Set zoomEnabled to YES
  2. Implement a scrollView delegate

Please check the Apple documentation which says:

For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.

Upvotes: 0

Related Questions