简体   繁体   English

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

[英]How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview

I'm on a project that makes an custom 'Move and Scale' Controller of UIImagePickerController . 我正在开发一个项目,它可以自定义UIImagePickerController 'Move and Scale' Controller (controller when appear if imagePickerController.allowsEditing=YES ) (如果imagePickerController.allowsEditing=YES ,则显示控制器)

I want to crop an UIImage in the crop rectangular like the picture below. 我想在裁剪矩形crop an UIImage ,如下图所示。

截图1

And I made some code to set contentInset . 我做了一些代码来设置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

and here is the result. 这是结果。

截图2

Black region is contentInset area. 黑色区域是contentInset区域。

However, if I pinch this scrollView to zoom in, something happen. 但是,如果我捏这个scrollView进行放大,就会发生一些事情。

截图3

I think I have to do something on 我想我必须做些什么

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

to adjust contentInset dynamically. 动态调整contentInset。 How can I do this? 我怎样才能做到这一点? Please give me some help :) 请给我一些帮助:)

I hoped someone answered this question, I'm sad because it's not. 我希望有人回答这个问题,我很难过,因为事实并非如此。

But thank God, I solved this. 但是感谢上帝,我解决了这个问题。

in

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

adjust contentInset dynamically using zoomScale. 使用zoomScale动态调整contentInset。 I just implement only Lanscape mode for testing, but Portrait mode is exactly the same way. 我只是实现了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);
}

And bam. 和bam。 here is the screenshot. 这是截图。

截图4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM