title: Passing an existing and active gesture recognizer to a modal view controller
body: <p>I want to launch a modal view with a long press, and then dismiss the modal view when the long press is cancelled. How do I do this?</p>
  
  <ul>
  <li>I tried passing the longPressRecognizer to the modal view and
  setting it as a delegate, but that didn't work.</li>
  <li>I tried something simpler- detecting a touchesEnded, which would mean in the modal view a touch ended, but that doesn't fire either.</li>
  </ul>
  
  <p>Is there a way to tell the modal view a gesture has started; I want you to recognize the end/cancellation of this gesture or a touch?</p>
  
  <hr>
  
  <p><strong>ViewController.swift</strong></p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UIGestureRecognizerDelegate {
      var buttonView:UIView!
      var longPressRecognizer:UILongPressGestureRecognizer!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
          self.longPressRecognizer.delegate = self
          self.view.addGestureRecognizer(self.longPressRecognizer)
  
          // Add a button
          buttonView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100))
          buttonView.userInteractionEnabled = true;
          buttonView.backgroundColor = UIColor.grayColor()
          self.view.addSubview(buttonView)
      }
  
      func longPressed(recognizer: UILongPressGestureRecognizer) {
          let point: CGPoint = recognizer.locationInView(self.view)
          if let pressedView = self.view.hitTest(point, withEvent: nil) {
              if pressedView == self.buttonView {
                  switch recognizer.state {
                  case .Began:
                      NSLog("long pressed - Began")
                      var mediaViewController = MediaViewController()
                      self.presentViewController(mediaViewController, animated: false, completion: nil)
                  case .Cancelled:
                      NSLog("long pressed - Cancelled")
                  case .Ended:
                      NSLog("long pressed - Ended")
                  default:
                      break
                  }
              }
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>
  
  <p><strong>MediaViewController.swift</strong></p>
  
  <pre><code>import UIKit
  
  class MediaViewController: UIViewController, UIGestureRecognizerDelegate {
      var longPressRecognizer:UILongPressGestureRecognizer!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          self.view.backgroundColor = UIColor.lightGrayColor()
  
          self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
          self.longPressRecognizer.delegate = self
          self.view.addGestureRecognizer(self.longPressRecognizer)
      }
  
      func longPressed(recognizer: UILongPressGestureRecognizer) {
          switch recognizer.state {
          case .Began:
              NSLog("long pressed - Began")
          case .Cancelled:
              NSLog("long pressed - Cancelled")
          case .Ended:
              NSLog("long pressed - Ended")
              self.dismissViewControllerAnimated(false, completion: nil)
          default:
              break
          }
      }
  
      override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
          NSLog("touches began")
      }
  
      override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
          NSLog("touches ended")
      }
  
  }
  </code></pre>