![](/img/trans.png)
[英]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.