如何在UIScrollView中捏缩放多个UIImageViews

时间:2013-12-04 11:54:32

标签: ios iphone uiscrollview uiimageview pinchzoom

我必须捏缩放多张图像。我已经将每个UIImageView添加到UIView并将UIView添加到UIScrollView。并返回UIView图像viewForZoomingInScrollView:委托方法,但图像不按预期缩放。有没有更好的方法?

2 个答案:

答案 0 :(得分:1)

#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

答案 1 :(得分:0)

  • 检查滚动视图的最小和最大缩放比例属性。 (最大值应大于min,以便进行缩放)
  • 确保您返回相应的UIImageView而不是UIView
  • 确保已将滚动视图委托设置为要向其发送滚动视图的委托消息的对象,例如viewForZoomingInScrollView: