在iPhone中使用UIPichGestureRecognizer执行picture后图像大小重置为原始大小

发布于 2024-12-10 07:21:42 字数 2365 浏览 0 评论 0原文

我有一个视图控制器,其中添加了 UIScrollView 和 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];
}

I have one view controller in which I have added UIScrollView & UIImageView programatically. I have added UIPichGestureRecognizer to the UIImageView. My UIImageView is added to UIScrollView as a subview.

My problem is when I try to pinch the image , it zoom in. But when I release the touches from screen it again come to its default size. I can not find the error in code. Please help me.
Below is my code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-12-17 07:21:42

据我所知,问题出在这里:

scrollViewForImage.maximumZoomScale = 1;

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

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

scrollViewForImage.maximumZoomScale = 3;

From what I can see the problem lies here:

scrollViewForImage.maximumZoomScale = 1;

You are setting the maximum zoom scale of the image to 1 x its full size. This means once you finish pinching the image, it will scale back to 1 x its size.

If you want to be able to zoom the image to a larger size, try settings this value to be higher than 1. e.g.

scrollViewForImage.maximumZoomScale = 3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文