Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to segue to view controller on panGesture

Tags:

ios

swift

segue

I'm trying to segue to a view controller. the segue's identifier is "Orange" (the other view controller is just an orange view). The view controller is segued through storyboard and connected to an image view in my main view controller. I want to trigger this with a pan gesture recognizer. So when a pan is detected, it just segues to the view controller.

This is my attempt:

@IBOutlet var panGesture: UIPanGestureRecognizer!

@IBAction func onPan(sender: UIPanGestureRecognizer) {
    if panGesture.state == .Ended {
        print("panned")
    performSegueWithIdentifier("Orange", sender: sender)
    }
}

However, this is not working. I know it does detect the pan, but it doesn't perform the segue.

I read how to present the segue from IOS - How to segue programmatically using swift

like image 772
Useful_Investigator Avatar asked Aug 29 '16 11:08

Useful_Investigator


People also ask

How do I segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How to use pan gesture recognizer in Swift?

A pan gesture occurs when the user moves one or more fingers around the device screen. UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer. It use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gesture.


1 Answers

Try presenting ViewController's by instantiation: -

  • Make sure you have a navigation Controller embed in your First VC

  • Giving your ViewController a StoryBoardID :- Select the ViewController in your storyboard which you want to present -> Identity Inspector -> StoryBoard ID Give your VC a storyboard ID lets say : "vc2SceneVC_ID"

  • Instantiate and push the VC like this:-

    @IBAction func onPan(sender: UIPanGestureRecognizer) {
    
    if panGesture.state == .Ended {
    print("panned")
    
     let vc2Scene = self.storyboard?.instantiateViewControllerWithIdentifier("vc2SceneVC_ID") as! VC2
     self.navigationController?.pushViewController(vc2Scene, animated: true)
    
    }
    

Apple Documentation for Navigation Controller :- UINavigationController Apple Doc

PS:- Prefer instantiating the navigating through ViewControllers with instantiation rather performSegueWithIdentifier("Orange", sender: sender) Because this way you get to transfer data back-forth those viewControllers, also it prevents Memory Leaks in most cases.

like image 104
Dravidian Avatar answered Sep 21 '22 01:09

Dravidian