4

I am doing a project in which i need two imageviews in single Scrollview with zooming options. UIScrollView allows one view to make zoom easily. But my problem is not like that. I have two imageviews side by side in the scroll view.When I try to zoom the images its not working Is there is any option to zoom two image views at the same time.Please suggest me some idea.

Thanks in advance

btmanikandan
  • 1,923
  • 2
  • 25
  • 45
  • so your problem is that when you zoom, your two images should be zoom...Am i right??? – NiravPatel Aug 23 '13 at 05:09
  • 1
    i think you should add two imageview in one view and in the viewForZooming method you should return thats view – Sunny Shah Aug 23 '13 at 05:24
  • Put your `UIImageView`s in two separate `UIScrollView` and put those `UIScrollView` in your `UIScrollView` and set tag for those and use the delegate to zoom it as in the @Sunnyshah mentioned after checking the tag for `UIScrollView`. Hope it will work. – Exploring Aug 23 '13 at 06:07
  • Ya I have done like that only but it is not working – btmanikandan Aug 23 '13 at 09:37

4 Answers4

3

Here is code for Zooming two images in single scrollview

I have added two UIImageView's in One UIView and then I have added the UIView to UIScrollView. Following is my code :

- (void)viewDidLoad {
    [super viewDidLoad];
    self.vwImages = [[UIView alloc]initWithFrame:CGRectMake(0, 0,100,200)];

    // 1
    UIImage *image = [UIImage imageNamed:@"imgBaskteBall2.jpeg"];
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image];
    imageView1.frame = CGRectMake(0,0,100,100);
    [self.vwImages addSubview:imageView1];

    UIImage *image2 = [UIImage imageNamed:@"imgBaskteBall1.jpeg"];
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
    imageView2.frame = CGRectMake(0, 100, 100, 100);
    [self.vwImages  addSubview:imageView2];

    // 2

    [self.scrollView addSubview:self.vwImages];

    self.scrollView.contentSize = self.vwImages.frame.size;

    // 3
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    [self.scrollView addGestureRecognizer:doubleTapRecognizer];

    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    [self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // 4
    CGRect scrollViewFrame = self.scrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
    CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
    CGFloat minScale = MIN(scaleWidth, scaleHeight);
    self.scrollView.minimumZoomScale = minScale;

    // 5
    self.scrollView.maximumZoomScale = 10.0f;
    self.scrollView.zoomScale = minScale;

    // 6
    [self centerScrollViewContents];
}


- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.vwImages.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.vwImages.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // 1
    CGPoint pointInView = [recognizer locationInView:self.imageView];

    // 2
    CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);

    // 3
    CGSize scrollViewSize = self.scrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);

    // 4
    [self.scrollView zoomToRect:rectToZoomTo animated:YES];
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
    [self.scrollView setZoomScale:newZoomScale animated:YES];
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.vwImages;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // The scroll view has zoomed, so you need to re-center the contents
    [self centerScrollViewContents];
}
Unnati
  • 2,441
  • 17
  • 37
Nevil Lad
  • 533
  • 6
  • 20
3

Make object of UIView as container for example viewContainer

[viewContainer addSubView:imageView1];

[viewContainer addSubView:imageView2];

[scrollView addSubView:viewContainer];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return viewContainer;
}

Add

Praveen Gowda I V
  • 9,569
  • 4
  • 41
  • 49
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
0

You can use this

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return [self.scrollView.subviews objectAtIndex:0];
}

the viewForZoomingInScrollView delegate method return the object at Index 0.

From Properly zooming a UIScrollView that contains many subviews

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
0

add both the imageView in the view and replace this method

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return view;
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86