我在故事板中设置imageView
,其中只有一个imageView
使用自动布局,如here所示。
此外,我按照docs中的说明启用了此imageView
的缩放缩放功能。
scollView
对scrollView
的边有4个0的约束,它与主视图的宽度和高度相等,其中imageView
为子视图。
aspectFit
具有('#parent children').remove()
缩放模式,因此在未缩放状态下,图像将从左到右,或从上到下(或两者)完全延伸。
在下面的示例中,它从上到下延伸,留下一些左右可见的背景:
我可以向上或向下移动缩放的图像,但移动在背景在顶部或底部可见之前停止。这是我想要的。
如何避免到目前为止(此处,右侧)移动图像。与顶部或底部一样,它应该停止在边界移动,这样就不会有背景变得可见。
答案 0 :(得分:2)
不是理想的解决方案,但我希望它会有所帮助:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
[self adjustScrollViewInsets];
}
- (void)adjustScrollViewInsets {
CGFloat imageWidth = self.imageView.image.size.width;
CGFloat imageHeight = self.imageView.image.size.height;
CGFloat aspect = imageWidth / imageHeight;
CGSize imageViewSize = self.imageView.frame.size;
if (imageViewSize.width / aspect <= imageViewSize.height) {
[self adjustVerticalInsetsWithImageHeight:(imageViewSize.width / aspect)];
} else {
[self adjustHorizontalInsetsWithImageWidth:(imageViewSize.height * aspect)];
}
}
- (void)adjustHorizontalInsetsWithImageWidth:(CGFloat)width {
CGFloat horizontalInset = (self.scrollView.contentSize.width - width) / 2;
if (width < self.scrollView.frame.size.width) {
horizontalInset = horizontalInset - (self.scrollView.frame.size.width - width) / 2;
}
[self.scrollView setContentInset:UIEdgeInsetsMake(0, -horizontalInset, 0, -horizontalInset)];
}
- (void)adjustVerticalInsetsWithImageHeight:(CGFloat)height {
CGFloat verticalInset = (self.scrollView.contentSize.height - height) / 2;
if (height < self.scrollView.frame.size.height) {
verticalInset = verticalInset - (self.scrollView.frame.size.height - height) / 2;
}
[self.scrollView setContentInset:UIEdgeInsetsMake(-verticalInset, 0, -verticalInset, 0)];
}
如果您遇到麻烦,请查看this repo。
答案 1 :(得分:0)
我遇到了同样的问题,并在Swift 4.0中实现,如下所示,它对我有用。
Swift 4.0:
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
adjustScrollViewInsets()
}
func adjustScrollViewInsets() {
let imageWidth = self.imageView.image?.size.width
let imageHeight = self.imageView.image?.size.height
let aspect = imageWidth! / imageHeight!
let imageViewSize = self.imageView.frame.size
if imageViewSize.width / aspect <= imageViewSize.height {
adjustVerticalInsetsWithImageHeight(height: imageViewSize.width / 2)
} else {
adjustHorizontalInsetsWithImageWidth(width: imageViewSize.height / 2)
}
}
func adjustHorizontalInsetsWithImageWidth(width: CGFloat) {
var horizontalInset = (self.scrollView.contentSize.width - width) / 2
if width < self.scrollView.contentSize.width {
horizontalInset = horizontalInset - (self.scrollView.frame.size.width - width) / 2
}
self.scrollView.contentInset = UIEdgeInsetsMake(0, -horizontalInset, 0, -horizontalInset)
}
func adjustVerticalInsetsWithImageHeight(height: CGFloat) {
var verticalInset = (self.scrollView.contentSize.height - height) / 2
if height < self.scrollView.frame.size.height {
verticalInset = verticalInset - (self.scrollView.frame.size.height - height) / 2
}
self.scrollView.contentInset = UIEdgeInsetsMake(-verticalInset, 0, -verticalInset, 0)
}