0

I can't seem to get this to work programatically. I have a UIImageView which I want to make Draggable and Pinchable. Everything for the draggable part is working, the pinch on the other hand doesn't seem to work.

My Custom UIImageView Class subclasses UIImageView and Implements UIGestureRecognizerDelegate

In my init functions I have the following:

required init?(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)

    self.initializeGestures()

}


override init(image: UIImage?) {

    super.init(image: image)

    self.initializeGestures()

}

func initializeGestures() {

    self.userInteractionEnabled = true
    self.multipleTouchEnabled = true

    dragGesture = UIPanGestureRecognizer(target: self, action: "handlePan:")
    zoomGesture = UIPinchGestureRecognizer(target: self, action: "handlePinch:")

    self.addGestureRecognizer(dragGesture)
    self.addGestureRecognizer(zoomGesture)

}

Followed by the following lines of code:

func handlePan(recognizer: UIPanGestureRecognizer!) {

    Scripts.log("PAN >>> DRAG ACTIVATED")
}


func handlePinch(recognizer: UIPinchGestureRecognizer) {

    Scripts.log("PINCH >>> ZOOM ACTIVATED")

    recognizer.view?.transform = CGAffineTransformScale((recognizer.view?.transform)!, recognizer.scale, recognizer.scale)
    recognizer.scale = 1

}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    return true

}
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • add pinch gesture into pan gesture dependencies, – SolaWing Dec 28 '15 at 04:20
  • @SolaWing what do you mean by that? – Lavvo Dec 28 '15 at 04:32
  • two gesture conflict, to recognize pinch gesture, you need to add a dependency to pan gesture. so when pinch recognize failed, pan can be recognized. – SolaWing Dec 28 '15 at 04:35
  • @SolaWing I made some edits to the code and tried doing this if this is what you mean, but then they both don't work now -> dragGesture.requireGestureRecognizerToFail(zoomGesture) – Lavvo Dec 28 '15 at 04:58
  • forgive my error, pinch need 2 touch and can't be fail with 1 touch. After I test, your need add `dragGesture.delegate = self`, so shouldRecognizeSimultaneouslyWithGestureRecognizer will be call – SolaWing Dec 28 '15 at 05:36
  • @SolaWing Now it is working! Thank you very much!! I wish I could give you credit for the answer since you just replied to the comment, but thanks. – Lavvo Dec 28 '15 at 11:56

0 Answers0