如何限制捏缩放图像到图像边框的移动?

时间:2015-11-17 14:35:10

标签: ios uiscrollview autolayout

我在故事板中设置imageView,其中只有一个imageView使用自动布局,如here所示。 此外,我按照docs中的说明启用了此imageView的缩放缩放功能。

scollViewscrollView的边有4个0的约束,它与主视图的宽度和高度相等,其中imageView为子视图。

aspectFit具有('#parent children').remove()缩放模式,因此在未缩放状态下,图像将从左到右,或从上到下(或两者)完全延伸。
在下面的示例中,它从上到下延伸,留下一些左右可见的背景:
enter image description here

我可以捏缩放图像,使其大于屏幕:
enter image description here

我可以向上或向下移动缩放的图像,但移动在背景在顶部或底部可见之前停止。这是我想要的。

但是,我可以向左或向右移动缩放图像,以便可以看到背景:
enter image description here

如何避免到目前为止(此处,右侧)移动图像。与顶部或底部一样,它应该停止在边界移动,这样就不会有背景变得可见。

2 个答案:

答案 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)
}