我设置了UIScrollView
,其中包含UIImageView
和UIImage
。我加载到图像视图中的UIImage大小约为2300x1200,不能缩放,因为可以缩放。UIScrollView的布局不正确,UIScrollView在IOS的方向更改后保存UIImageView
UIScrollView包含一个UIImageView,它被设置为允许点击/双击来放大和缩小图像。我有followed a tutorial here to create this。
该示例的源代码可以是downloaded here.。
问题
我遇到的问题是改变方向。意见不再按预期排列并抵消。
一旦下载图像(在我的例子),我完成以下步骤:
[self.mainImageView setImage:image];
self.mainImageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
// Tell the scroll view the size of the contents
self.mainScrollView.contentSize = image.size;
[self setupScales];
然后我setupScales如下:
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.mainScrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width/self.mainScrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height/self.mainScrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.mainScrollView.minimumZoomScale = minScale;
self.mainScrollView.maximumZoomScale = 1.0f;
self.mainScrollView.zoomScale = minScale;
[self centerScrollViewContents];
然后我centerScrollViewContents如下:
CGSize boundsSize = self.mainScrollView.bounds.size;
CGRect contentsFrame = self.mainImageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.mainImageView.frame = contentsFrame;
scrollView和imageView使用以下设置:
self.mainScrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.mainImageView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView对左侧/右侧/顶部/底部约束固定为0,并且imageView没有手动创建的约束。我试图在willRotateToInterfaceOrientation
中运行self.mainScrollView needsUpdateConstraints
,但这并没有什么区别。
当选择第一个选项“图像缩放”,然后更改设备的方向时,可以在上面的示例应用程序中复制相同的问题。我不确定什么是不正确的,它出现的框架设置不正确。我试图在不同的点运行setupScales,但这似乎并没有改变这个问题。