UIImageView缩小缩放

前端之家收集整理的这篇文章主要介绍了UIImageView缩小缩放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望有人可以帮助我。我试图允许用户将UIImageView缩放(允许使用最大和最小级别)。但是由于某种原因,它不正常。图像放大一点,然后弹回。谢谢。

这里是放大功能

func zoom(sender:UIPinchGestureRecognizer) {


    if sender.state == .Ended || sender.state == .Changed {

        let currentScale = self.view.frame.size.width / self.view.bounds.size.width
        var newScale = currentScale*sender.scale

        if newScale < 1 {
            newScale = 1
        }
        if newScale > 9 {
            newScale = 9
        }

        let transform = CGAffineTransformMakeScale(newScale,newScale)

        self.imageView?.transform = transform
        sender.scale = 1

    }

}
我决定将imageView添加到UIScrollView。它允许用户缩放和平移。这是我使用的代码

为了设置最大/最小缩放我使用:

scrollImg.minimumZoomScale = 1.0
    scrollImg.maximumZoomScale = 10.0

这里是其余的代码

var vWidth = self.view.frame.width
    var vHeight = self.view.frame.height

    var scrollImg: UIScrollView = UIScrollView()
    scrollImg.delegate = self
    scrollImg.frame = CGRectMake(0,vWidth!,vHeight!)
    scrollImg.backgroundColor = UIColor(red: 90,green: 90,blue: 90,alpha: 0.90)
    scrollImg.alwaysBounceVertical = false
    scrollImg.alwaysBounceHorizontal = false
    scrollImg.showsVerticalScrollIndicator = true
    scrollImg.flashScrollIndicators()

    scrollImg.minimumZoomScale = 1.0
    scrollImg.maximumZoomScale = 10.0

    defaultView!.addSubview(scrollImg)

    imageView!.layer.cornerRadius = 11.0
    imageView!.clipsToBounds = false
    scrollImg.addSubview(imageView!)

我也不得不添加这一点

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return self.imageView
}

猜你在找的Swift相关文章