[英]Center UIImageView inside UIScrollView without contentInset?
[英]How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview
我正在开发一个项目,它可以自定义UIImagePickerController
'Move and Scale' Controller
。 (如果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。 我怎样才能做到这一点? 请给我一些帮助:)
我希望有人回答这个问题,我很难过,因为事实并非如此。
但是感谢上帝,我解决了这个问题。
在
- (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);
}
和bam。 这是截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.