【问题标题】:How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview如何在 scrollViewDidZoom 中动态调整 contentInset?我最初在 UIScrollview 中设置 UIImageView 的 contentInset
【发布时间】:2013-04-10 14:59:30
【问题描述】:

我正在进行一个自定义 'Move and Scale' ControllerUIImagePickerController 的项目。 (imagePickerController.allowsEditing=YES时出现的控制器)

我想在裁剪矩形中crop an UIImage,如下图所示。

我编写了一些代码来设置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

这是结果。

黑色区域是 contentInset 区域。

但是,如果我捏这个 scrollView 来放大,就会发生一些事情。

我想我必须做点什么

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

动态调整 contentInset。我怎样才能做到这一点?请给我一些帮助:)

【问题讨论】:

  • 不是您问题的真正答案,但您的方法与苹果的不同,这就是问题所在。当图像完全缩小时,它们不允许向下滚动,使用捏合手势时不能只放大图像的上部。您要么需要将方法更改为 Apple 使用的方法并限制缩放区域,要么使用一些 UI 元素以编程方式执行缩放,这样您就可以完全控制正在缩放的​​区域。
  • @Eugene 谢谢你的评论。我认为我的方法与 Apple 的方法完全相同。我模仿默认的 UIImagePickerController。我只想实现默认的 UIImagePickerController 'Move and Scale' 控制器。您能解释一下为什么这与 Apple 的复杂程度不同吗?

标签: iphone ios objective-c uiscrollview uiimageview


【解决方案1】:

我希望有人回答这个问题,我很伤心,因为它不是。

但感谢上帝,我解决了这个问题。

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

使用 zoomScale 动态调整 contentInset。我只是实现Lanscape mode 进行测试,但Portrait mode 是完全相同的方式。

// 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);
}

然后砰。这是屏幕截图。

【讨论】:

    猜你喜欢
    • 2010-11-29
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2019-05-25
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    相关资源
    最近更新 更多