4

I have a UIPinchGestureRecognizer that allows a user to scale a UITextView. That all works with the following code:

@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
      view.transform = CGAffineTransformScale(view.transform,
        recognizer.scale, recognizer.scale)

        recognizer.scale = 1
    }
  }

However, I only want the user to be able to make the UITextView larger, not smaller. I have set my UIGestureRecognizerDelegate and have put in the following code:

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let pinch = gestureRecognizer as? UIPinchGestureRecognizer {
        return pinch.scale < 1
    }
    return true
}

I got that code from this post here: UIPinchGestureRecognizer only for pinching out.

However, my method never gets called and it is not working. Any thoughts?

Community
  • 1
  • 1
B. Wolf
  • 81
  • 1
  • 5
  • 4
    Did you ever set `gestureRecognizer.delegate` to `self`? – keithbhunter Nov 18 '15 at 21:00
  • It was not set. I just set it and it's still allowing me make it both smaller and larger. HOWEVER, it is finally hitting my breakpoint! So the method is being called now. Just not acting the way I need it to. – B. Wolf Nov 18 '15 at 21:07
  • Hey OP! Please consider accepting an answer to markt his question as resolved. – LinusGeffarth Jan 17 '19 at 07:05

1 Answers1

10

Make sure to include UIGestureRecognizerDelegate in your class declaration:

class ViewController: UIViewController, UIGestureRecognizerDelegate { /* ... */ }

Then, set your gestureRecognizer's delegate to self:

gestureRecognizer.delegate = self

Hope that helps :)

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174