更改子视图后 UIScrollView 不尊重 minimumZoomScale [英] UIScrollView not respecting minimumZoomScale after changing the subview
问题描述
我遇到了一个问题,让 UIScrollView 正确更新以响应最小缩放比例的变化.
I'm hitting a problem getting a UIScrollView to update correctly in response to a change in the minimum zoom scale.
scrollview 有一个 UIImageView 作为子视图,UIImageView 的 image 属性是响应 UIPickerView 的 didFinishPickingMediaWithInfo 方法设置的:
The scrollview has a UIImageView as a subview, and the image property of the UIImageView is set in response to the didFinishPickingMediaWithInfo method of UIPickerView:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *takenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:takenImage];
[self.imageView setFrame:CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, takenImage.size.width, takenImage.size.height)];
[self.scrollView setContentSize:CGSizeMake(takenImage.size.width, takenImage.size.height)];
[self.scrollView setMinimumZoomScale:[self.scrollView frame].size.width / takenImage.size.width];
[self.scrollView setMaximumZoomScale:2.0];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale] animated:YES];
[self dismissModalViewControllerAnimated:YES];
}
第一次使用此方法添加图像时,这可以正常工作.但是,如果再次触发此方法 - 即使添加与第一次相同的图像 - 后续图像在 scrollView 中以全尺寸显示,并且无法缩小 - 只能放大.
This works correctly the first time an image is added using this method. However, if this method is triggered again - even to add the same image as the first time - the subsequent image is displayed at full size in the scrollView, and can't be zoomed out - only zoomed in.
我已将 scrollView 的 contentSize
、zoomScale
、minimumZoomScale
和 maximumZoomScale
转储到日志中,他们每次都是一样的.minimumZoomScale
每次都被正确计算.
I've dumped the contentSize
, zoomScale
, minimumZoomScale
and maximumZoomScale
of the scrollView to the log, and they are the same each time. The minimumZoomScale
is being calculated correctly each time.
就好像使用 1.0 的 minimumZoomScale
重绘 scrollView 一样,忽略了它已显式设置的事实.有什么明显的东西我在这里遗漏了吗?
It's as if the scrollView is being redrawn with a minimumZoomScale
of 1.0, ignoring the fact it's been explicitly set. Is there something obvious that I'm missing here?
推荐答案
问题似乎是 - 不确定此行为是否记录在某处 - 在当前缩放比例设置为 1.0 以外的其他值时重置内容大小.
The problem seems to be - not sure if this behavior is documented somewhere - resetting the content size while the current zoom scale is set to something other than 1.0.
修复很简单:在设置新内容大小之前,将 zoomScale
属性重置为 1.0 :
The fix is simple: Reset the zoomScale
property to 1.0 before setting the new content size:
// See http://stackoverflow.com/questions/10586577
// first reset before new content size
scroller.zoomScale = 1.0f;
// set new content size
scroller.contentSize = newImage.size;
// ... adjust minimum/maximum zoom scales if needed
这篇关于更改子视图后 UIScrollView 不尊重 minimumZoomScale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!