UIImageView捏快速缩放

时间:2019-07-03 10:02:13

标签: ios swift uiscrollview uiimageview

我正在使用下面的代码缩放图像,但是我的imageView越来越缩放到4比例因子以上,然后我尝试将imageView拖动到缩小到4比例因子。

如何阻止其缩放到4 scaleFactor之上,或者如何在拖动时阻止其缩放?

class ZoomViewController: UIViewController,UIScrollViewDelegate {

@IBOutlet weak var scrollView:UIScrollView!
@IBOutlet weak var imageView:UIImageView!

override func viewDidLoad() {

        super.viewDidLoad()
        scrollView.delegate = self

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 4.0
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
}

1 个答案:

答案 0 :(得分:0)

您可以通过实现UIScrollViewDelegate's scrollViewDidZoom(_:)方法,即

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    if scrollView.zoomScale > scrollView.maximumZoomScale {
        scrollView.zoomScale = scrollView.maximumZoomScale
    } else if scrollView.zoomScale < scrollView.minimumZoomScale {
        scrollView.zoomScale = scrollView.minimumZoomScale
    }
}

在上面的代码中,检查scrollView's zoomScale是否位于maximumZoomScaleminimumZoomScale内。如果没有,请按照允许的限制进行更新。