Search code examples
iosiphoneswiftsegueuipinchgesturerecognizer

Attempting to present <**> on <**> while a presentation is in progress. While performing a segue from one view controller to another using swift


override func viewDidAppear(animated: Bool) {

    view.userInteractionEnabled = true
    let pinchGesture:UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: "pinchGesture")
    view.addGestureRecognizer(pinchGesture)

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
    view.endEditing(true)
}

func pinchGesture(){

    self.performSegueWithIdentifier("trick1Segue", sender: self)

}

In my iOS app, i want to transition to a different view controller when a pinch gesture is performed on the screen. The tap gesture is to dismiss the keyboard when tapped outside the keyboard area

While running the app, I get an error message:

"Attempting to present < ** > on < ** > while a presentation is in progress"

The new view controller appears but opens twice, with a very short time difference. Looked up a lot of blogs but couldn't find a solution, please help!!!


Solution

  • The problem is that pinchGesture can be called multiple times. You should add a property to your viewController to keep track of the fact that you already have acted upon the pinch gesture:

    var segueInProcess = false
    
    func pinchGesture() {
        if !segueInProcess {
            self.performSegueWithIdentifier("trick1Segue", sender: self)
            segueInProcess = true
        }
    }