Search code examples
iosswiftuipinchgesturerecognizer

UIPinchGestureRecognizer is firing twice


I am adding UIPinchGestureRecognizer to a scrollView which shows images. On pinch, I am presenting a new view.

var pinch = UIPinchGestureRecognizer(target: self, action: "showFullScreen:")
self.scrollView.addGestureRecognizer(pinch)

The showFullScreen function:

func showFullScreen(sender:UITapGestureRecognizer) {     
    presentViewController(photoBro, animated: true, completion: nil)
}

But when I pinch on scrollView, showFullScreen is called twice because of which the following error comes:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller

I was searching for solution, and they suggest to remove the pinchGesture, but I want to keep the gestureRecognizer, so that users can still pinch to enter full screen.

What can I do to ensure that showFullScreen is called only once for one pinch?


Solution

  • try this:

      func showFullScreen(sender:UITapGestureRecognizer) {     
         if(sender.state == UIGestureRecognizerState.Ended) {
             presentViewController(photoBro, animated: true, completion: nil)
         }    
      }