简体   繁体   中英

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 . (controller when appear if imagePickerController.allowsEditing=YES )

I want to crop an UIImage in the crop rectangular like the picture below.

截图1

And I made some code to set 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.

However, if I pinch this scrollView to zoom in, something happen.

截图3

I think I have to do something on

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

to adjust contentInset dynamically. 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. I just implement only Lanscape mode for testing, but Portrait mode is exactly the same way.

// 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. here is the screenshot.

截图4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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