Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView pinch zoom swift

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 work right. The image zooms a little then just bounces back. Thank you.

here is the zoom func

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      }  } 
like image 416
RandomBytes Avatar asked May 03 '15 13:05

RandomBytes


People also ask

How do you use pinch zoom?

Pinch-to-zoom refers to the multi-touch gesture that zooms in or out of the displayed content on a device with a touch screen. These devices include a smartphones and tablets. To use pinch-to-zoom, touch two fingers on the touch screen, and move them apart to zoom in, or together to zoom out.


2 Answers

UIImageView pinch zoom with UIScrollView || image zooming ios in swift 3 and Xcode 8 letter Youtube video URL

set uiscrollview Delegate in storyboard enter image description here

 class PhotoDetailViewController: UIViewController, UIScrollViewDelegate {      @IBOutlet weak var scrollView: UIScrollView!     @IBOutlet weak var imgPhoto: UIImageView!            override func viewDidLoad() {                  super.viewDidLoad()              scrollView.minimumZoomScale = 1.0         scrollView.maximumZoomScale = 6.0                 // scrollView.delegate = self - it is set on the storyboard.     }                func viewForZooming(in scrollView: UIScrollView) -> UIView? {                 return imgPhoto     } 
like image 159
4 revs, 2 users 83% Avatar answered Sep 23 '22 13:09

4 revs, 2 users 83%


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 } 
like image 22
RandomBytes Avatar answered Sep 21 '22 13:09

RandomBytes