Pinch, Pan and Double tap gesture on UIImageView

1000 Views Asked by At

I want to apply these gestures on an imageview with following restrictions:

1)Double Tap: Zoom image two times and then return to original size

2)Pinch: Zoom image with a maximum limit and minimum limit to original size

3)Pan: When image is zoomed move image and image view should be not pan further than one of the boundaries

I have tried the following code using tutorials but pan does not work properly which I think sometimes affects others gestures too:

Please let me know what I am doing wrong.

Properties:

var originalFrame = CGRect()
var initialCenter = CGPoint()  // The initial center point of the view.
var imgTapCount = 0
var lastScale: CGFloat = 1.0
var minScale = 1.0
var maxScale = 4.0
var imageCurrentScale = 1.0

In viewDidLoad() I call following function

func displayImage() {
   //imgView gets image assigned here from Url
    originalFrame = imgView.frame
}

Tap Gesture:

@IBAction func imgTapGesture(_ sender: UITapGestureRecognizer) {
        guard sender.view != nil else {return}
        if imgTapCount < 2 {
            let pinchCenter = CGPoint(x: sender.location(in: view).x - view.bounds.midX,
                                      y: sender.location(in: view).y - view.bounds.midY)
            let transform = sender.view?.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
                .scaledBy(x: 2, y: 2)
                .translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
            sender.view?.transform = transform!
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                sender.view?.transform = CGAffineTransform.identity
            })
        }
        if imgTapCount < 2 {
            imgTapCount += 1
        } else {
            imgTapCount = 0
        }
    }

Pan Gesture

@IBAction func imgPanGesture(_ sender: UIPanGestureRecognizer) {
    guard sender.view != nil else {return}
    let piece = sender.view!
    // Get the changes in the X and Y directions relative to
    // the superview's coordinate space.
    let translation = sender.translation(in: piece.superview)
    if sender.state == .began {
        // Save the view's original position.
        self.initialCenter = piece.center
    }
    // Update the position for the .began, .changed, and .ended states
    if sender.state != .cancelled {
        // Add the X and Y translation to the view's original position.
       
        if (imgView.frame.width > piece.superview!.frame.width || imgView.frame.height > piece.superview!.frame.height) && (((imgView.frame.minX <= originalFrame.minX) || (imgView.frame.maxX >= originalFrame.maxX)) || (imgView.frame.minY <= originalFrame.minY) || (imgView.frame.maxY >= originalFrame.maxY)) {
            let newCenter = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + translation.y)
            print(newCenter)
            piece.center = newCenter
        }
    } else {
        // On cancellation, return the piece to its original location.
        piece.center = initialCenter
    }
}

Pinch Gesture

@IBAction func imgPinchGesture(_ sender: UIPinchGestureRecognizer) {
    var newScale = sender.scale
    if sender.state == .began {
        lastScale = self.imgView.frame.width/self.imgView.bounds.size.width
    }
    newScale = newScale * lastScale
    
    if newScale < minScale {
        newScale = minScale
    } else if newScale > maxScale {
        newScale = maxScale
    }
    
    let currentScale = self.imgView.frame.width/self.imgView.bounds.size.width
    self.imgView.transform = CGAffineTransform(scaleX: newScale, y: newScale)
        print("last Scale: \(lastScale), current scale: \(currentScale), new scale: \(newScale), gestureRecognizer.scale: \(sender.scale)")
}
1

There are 1 best solutions below

0
Zeeshan Ahmad II On
 lazy var pdfView: PDFView = {
    let pdfView = PDFView()
    pdfView.displaysPageBreaks = true
    pdfView.enableDataDetectors = true
    pdfView.subviews[0].backgroundColor = .clear
    pdfView.maxScaleFactor = 4
    return pdfView
}()

then after declaring pdf view you should add a func that convert image to pdfdocument

func createPDF(image: UIImage) -> PDFDocument {
 let document = PDFDocument()

 guard let cgImage = image.cgImage else { return document }

 let image = UIImage(cgImage: cgImage, scale: image.scale, orientation: .downMirrored)

 guard let imagePDF = PDFPage(image: image) else { return document }

 document.insert(imagePDF, at: document.pageCount)

 return document
}

after that in viewDidAppear after subview your pdf in view ... you can assign this pdfDocument to your pdf

pdfView.document = createPDF(image: "yourImage")
pdfView.autoScale = true