在IOS上单张ScrollView中缩放两个ImageView

时间:2013-08-23 05:04:25

标签: iphone ios uiscrollview pinchzoom

我正在做一个项目,我需要在单个Scrollview中使用缩放选项进行两次图像视图。 UIScrollView允许一个视图轻松进行缩放。但我的问题不是那样的。我在滚动视图中并排显示两个图像。当我尝试缩放图像时它不起作用是否可以同时缩放两个图像视图。请给我一些建议。

提前致谢

4 个答案:

答案 0 :(得分:3)

以下是在单个scrollview中缩放两张图片的代码

我在一个UIImageView中添加了两个UIView,然后我将UIView添加到UIScrollView。以下是我的代码:

- (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];
}

答案 1 :(得分:3)

将UIView的对象作为容器,例如viewContainer

[viewContainer addSubView:imageView1];

[viewContainer addSubView:imageView2];

[scrollView addSubView:viewContainer];

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

添加

答案 2 :(得分:0)

您可以使用此

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

viewForZoomingInScrollView委托方法返回索引0处的对象。

来自Properly zooming a UIScrollView that contains many subviews

答案 3 :(得分:0)

在视图中添加imageView并替换此方法

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