UIImageView pinch zoom swift

前端 未结 13 741
误落风尘
误落风尘 2020-11-28 04:06

I was hoping someone could help me out. I am trying to allow a user to pinch zoom on a UIImageView(with a max and min level allowed). But for some reason the it does not wor

13条回答
  •  难免孤独
    2020-11-28 04:56

    I decided to add the imageView to a UIScrollView. It allows the user to zoom and pan over. Here is the code I used.

    in order to set max/min zoom I used :

        scrollImg.minimumZoomScale = 1.0
        scrollImg.maximumZoomScale = 10.0
    

    here is the rest of the code.

        var vWidth = self.view.frame.width
        var vHeight = self.view.frame.height
    
        var scrollImg: UIScrollView = UIScrollView()
        scrollImg.delegate = self
        scrollImg.frame = CGRectMake(0, 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!)
    

    I also had to add this as well

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

    Swift 3 & above function prototype

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.mainImage
    }
    

提交回复
热议问题