如何在scrollViewDidZoom中动态调整contentInset? 我最初在UIScrollview中设置UIImageView的contentInset。

7
我正在进行一个项目,制作一个自定义的'移动和缩放'控制器,用于UIImagePickerController。(当imagePickerController.allowsEditing=YES时出现)。
我想要在像下面图片中那样的裁剪矩形中裁剪UIImage

screenshot 1

而我编写了一些代码来设置contentInset
    self.selectedImageView.backgroundColor = [UIColor redColor];

    CGRect cropRect = [SKPhotoCropFrameView getCropRectFromOrientation:self.photoCropOrientation];
    CGRect aspectFitRect = AVMakeRectWithAspectRatioInsideRect(self.selectedImageView.image.size, self.selectedImageView.frame);
    CGFloat difference = fabsf(cropRect.size.height - aspectFitRect.size.height);
    self.contentInset = UIEdgeInsetsMake(difference/2, 0, difference/2 + 20, 0);   // 20 is status bar height

这里是结果。

截图2

黑色区域是contentInset区域。

但是,如果我捏合scrollView放大,会发生一些事情。

截图3

我认为我必须在

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

如何动态调整contentInset?请帮我解决这个问题 :)


1
这并不是对你问题的回答,但你的方法和苹果的不同,这就是问题所在。当图像完全缩小时,他们不允许向下滚动,你不能只使用捏合手势放大图像的上部分。你需要改变你的方法,采用苹果的方式限制缩放区域,或者使用一些 UI 元素来以编程方式进行缩放,这样你就可以完全控制所缩放的区域。 - Eugene
@Eugene 谢谢您的评论。我认为我的方法与苹果公司的方法完全相同。我模仿了默认的UIImagePickerController。我只想实现默认的UIImagePickerController“移动和缩放”控制器。您能否解释一下这与苹果公司的区别更加复杂? - Wooseong Kim
1个回答

3
我希望有人能回答这个问题,但遗憾的是没有人回答。
但谢天谢地,我解决了这个问题。

之间。
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

使用zoomScale动态调整contentInset。我只是为了测试实现了横屏模式,但竖屏模式的方式完全相同。

// adjust contentInset
CGFloat increasingZoomScale = (scrollView.zoomScale == 1) ? 0 : (-1 * (1 - scrollView.zoomScale));

CGRect cropRect = [SKPhotoCropFrameView getCropRectFromOrientation:self.photoCropOrientation];
CGRect aspectFitRect = AVMakeRectWithAspectRatioInsideRect(self.selectedImageView.image.size, self.selectedImageView.frame);
CGFloat difference = fabsf(cropRect.size.height - aspectFitRect.size.height);

// implement at `Landscape` mode first
if (self.photoCropOrientation == SKPhotoCropOrientationPortrait) {

}else{
    // get scaledFrameHeight because it's `Landscape` crop mode
    CGFloat increasingFrameHeight = scrollView.frame.size.height * increasingZoomScale;
    self.contentInset = UIEdgeInsetsMake(difference/2 - increasingFrameHeight/2, 0, difference/2 - increasingFrameHeight/2, 0);
}

瞧,这就是截图。

screenshot 4


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接