Search code examples
iosswiftuinavigationcontrollersnapchat

SwipeNavigationController framework from GitHub how to implement


so my question today is how can I add a view similar to Snapchat where you use gestures to swipe to other views: left, top, bottom, right. I'm trying to use this framework but I'm not sure how to really implement it as they haven't provided a sample project. The layout of my app is I have a signup/login view controller, from there I want it to go to another view (blue) and that's the view that I want to have the different gestures mentioned above. `import UIKit import SwipeNavigationController

class BlueViewController: UIViewController {


let orangeVC = OrangeView()

let pinkVC = PinkView()
let greenVC = GreenView()
let purpleVC = PurpleView()



override func viewDidLoad() {



    let swipeNavigationController = SwipeNavigationController(centerViewController: self)
    swipeNavigationController.topViewController = self.pinkVC
    swipeNavigationController.bottomViewController = self.purpleVC
    swipeNavigationController.leftViewController = self.greenVC
    swipeNavigationController.rightViewController = self.orangeVC





    view.backgroundColor = UIColor.facebookBlueColor
   }
}'

I'm also not using storyboards for this project.


Solution

  • The SwipeNavigationController is a UIViewController and can be pushed onto a stack or presented the same as any other UIViewController. Wherever you are creating and presenting BlueViewController, you should instead create the SwipeNavigationController as the top level object that contains BlueViewController and all of the direction view controllers. BlueViewController and all of the other directions should not know anything about the SwipeViewController. BlueViewController and all of the other directions should not know anything about each other. The SwipeNavigationController is the top level view controller, all of the view controllers associated with a direction are child view controllers of it. I'm assuming that you have a navigation controller somewhere in your flow that pushes the SwipeNavigationController. In that case, you would have something like this in whatever method you want to trigger the push. I've called it nextTapped, but I'm sure it'll be something different in your code:

    func nextTapped() {
        let swipeNavigationController = SwipeNavigationController(centerViewController: BlueViewController())
        swipeNavigationController.topViewController = PinkViewController()
        swipeNavigationController.bottomViewController = PurpleViewController()
        swipeNavigationController.leftViewController = GreenViewController()
        swipeNavigationController.rightViewController = OrangeViewController()
    
        navigationController?.pushViewController(swipeNavigationController, animated: true)
    }
    

    And then remove everything from viewDidLoad in BlueViewController except for the line that sets the background color. This creates the SwipeNavigationController with all of the directional view controllers, keeping the BlueViewController as the center and then pushes it onto your view controller stack. If you don't have a UINavigationController in the view controller that is displayed before the SwipeNavigationController, you can present it modally by replacing the last line with this:

    present(swipeNavigationController, animated: true, completion: nil)