Search code examples
iphoneobjective-ciosuiscrollviewuiscrollviewdelegate

scrollview sub uiscrollviews uiimageview viewForZoomingInScrollView


I have main UIScrollView

mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
mainScrollView.pagingEnabled = YES;
imageScrollView.contentSize = CGSizeMake(320 * [myImage count], 440);

, which contain many sub UIScrollView, and each sub UIScrollView contains UIImageView

for (int i = 0; i < [myImage count]; i++)
{
    imageView = [[UIImageView alloc] init];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

    NSString *strimage = [myImage objectAtIndex:i];
    NSURL *imageUrl = [NSURL URLWithString:strimage];

    [imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"Placeholder"]];

    imageView.frame = CGRectMake(0, 0, 320, 440);

    subScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 440)];
    subScrollView.contentSize =
    CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    subScrollView.pagingEnabled = YES;
    subScrollView.showsHorizontalScrollIndicator = NO;
    subScrollView.showsVerticalScrollIndicator = NO;
    subScrollView.delegate = self;
    subScrollView.bouncesZoom = YES;
    subScrollView.clipsToBounds = YES;

    subScrollView.maximumZoomScale = 3.0;
    subScrollView.minimumZoomScale = 1.0;
    subScrollView.zoomScale = 1.0;

    [mainScrollView addSubview:subScrollView];
    [subScrollView addSubview:imageView];
}

, and I implement

#pragma mark UIScrollViewDelegate methods

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

The problem is when I zoom in UIImageView, assume that there are 3 sub UIScrollView, the last sub UIScrollView can zoom UIImageView normally but the other 2 when I zoom in it , it zooms sub UIScrollView instead of UIImageView, and it effect the last sub UIScrollView to be also zoom, Did I do anything wrong?

I suspect that in viewForZoomingInScrollView, it's zoom only last UIImageView (last sub UIScrollView).

Thank you in advance :)


Solution

  • Finally I figured it out, I just need to update my UIImageView when I zoom in specific UIImageView In - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView like this:

    #pragma mark -
    #pragma mark UIScrollViewDelegate methods
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        for (UIImageView *iv in [scrollView subviews]) {
            imageView = iv;
            break;
        }
    
        return imageView;
    }
    

    Hope it may help someone, who struggle at this point as me, so you don't have to waste you time :).