简体   繁体   中英

How to pinch zoom multiple UIImageViews inside UIScrollView

I have to pinch zoom multiple images. I have added each of the UIImageView to UIView and added the UIView to UIScrollView. and returning the UIView image viewForZoomingInScrollView: delegate method, but images are not zooming as expected. is there any better way?

#define VIEW_FOR_ZOOM_TAG (1)

@implementation [SVViewController][1]

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;

    CGRect innerScrollFrame = mainScrollView.bounds;

    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:  
        [NSString stringWithFormat:@"page%d", i + 1]]];
        imageForZooming.tag = VIEW_FOR_ZOOM_TAG;

        UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
        pageScrollView.minimumZoomScale = 1.0f;
        pageScrollView.maximumZoomScale = 2.0f;
        pageScrollView.zoomScale = 1.0f;
        pageScrollView.contentSize = imageForZooming.bounds.size;
        pageScrollView.delegate = self;
        pageScrollView.showsHorizontalScrollIndicator = NO;
        pageScrollView.showsVerticalScrollIndicator = NO;
        [pageScrollView addSubview:imageForZooming];

        [mainScrollView addSubview:pageScrollView];

        if (i < 2) {
            innerScrollFrame.origin.x += innerScrollFrame.size.width;
        }
    }

    mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + 
    innerScrollFrame.size.width, mainScrollView.bounds.size.height);

    [self.view addSubview:mainScrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
} 

- (BOOL)shouldAutorotate {
    return NO;
}

@end


  [1]: http://www.lyricspoints.com
  • Check the minimum and maximum zoom scale properties of the scroll view. (max should be greater than min for zoom to happen)
  • make sure that you are returning the corresponding UIImageView and not the UIView
  • Make sure that you have set the scroll view delegate as the object to which you want the delegate messages of the scroll view to be sent, like viewForZoomingInScrollView:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM