在缩放过程中防止图像尺寸减小小于100%

时间:2016-09-28 14:14:46

标签: swift uiscrollview uiimageview zoom

我的UIImageView包含在UIScrollView中。

scrollView.minimumZoomScale = 1.0

当我尝试缩小图像时,图像的大小等于50%。 当我移开手指时,图像的大小再次等于100%。

如何在缩放过程中防止缩小小于100%?

zoom out UIImageView

override func awakeFromNib() {
    super.awakeFromNib()
    scrollView.delegate = self
    scrollView.addSubview(imgView)
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 5.0
    scrollView.zoomScale = 1.0        
    centerScrollViewContents()
}

func centerScrollViewContents() {
    let boundsSize = scrollView.bounds.size
    var contentsFrame = imgView.frame

    if contentsFrame.size.width < boundsSize.width {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
    } else {
        contentsFrame.origin.x = 0.0
    }

    if contentsFrame.size.height < boundsSize.height {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
    } else {
        contentsFrame.origin.y = 0.0
    }

    imgView.frame = contentsFrame
}

0 个答案:

没有答案