使用iphone中的UIPinchGestureRecognizer执行音高后,图像尺寸重置为原始尺寸

时间:2011-10-13 14:30:01

标签: uiscrollview uiimageview uigesturerecognizer iphone-4

我有一个视图控制器,我在其中添加了UIScrollView& UIImageView以编程方式。我已将UIPichGestureRecognizer添加到UIImageView。我的UIImageView作为子视图添加到UIScrollView中。

我的问题是,当我试图捏合图像时,它会放大。但是当我从屏幕上释放触摸时,它再次达到其默认大小。我在代码中找不到错误。请帮我。 以下是我的代码

 - (void)createUserInterface {

    scrollViewForImage = [[UIScrollView alloc]initWithFrame:CGRectMake(20.0f, 60.0f, 280.0f, 200.0f)];
    scrollViewForImage.userInteractionEnabled = YES;
    scrollViewForImage.multipleTouchEnabled = YES;
    scrollViewForImage.backgroundColor = [UIColor redColor];
    scrollViewForImage.autoresizesSubviews = YES;
    scrollViewForImage.maximumZoomScale = 1;
    scrollViewForImage.minimumZoomScale = .50;
    scrollViewForImage.clipsToBounds = YES;
    scrollViewForImage.delegate = self;
    scrollViewForImage.bouncesZoom = YES;
    scrollViewForImage.contentMode = UIViewContentModeScaleToFill;
    [self.contentView addSubview:scrollViewForImage];

    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 200.0f)];
    [imageView setBackgroundColor:[UIColor clearColor]];
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        [pinchRecognizer setDelegate:self];
        [imageView addGestureRecognizer:pinchRecognizer];
    //[self.contentView addSubview:imageView];
    [self.scrollViewForImage addSubview:imageView];

    scrollViewForImage.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);

}


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

-(void)pinch:(id)sender {

    [self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
    lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

1 个答案:

答案 0 :(得分:1)

从我所看到的问题出在这里:

scrollViewForImage.maximumZoomScale = 1;

您正在将图像的最大缩放比例设置为其全尺寸的1倍。这意味着一旦完成对图像的捏合,它将缩小到其尺寸的1倍。

如果您希望能够将图像缩放到更大的尺寸,请尝试将此值设置为高于1.例如

scrollViewForImage.maximumZoomScale = 3;