Swift: show a different view controller when scrolling up in the first controller view

Hi, I checked a lot of questions regarding scrolling in SO, but you have doubts.

In my application I have two pages 1. User controller 2. Question viewing controller.

looks like that: userpage

now what I want to implement is to show the question view controller, while take the users screen below.

I am new to Ios, so I will help me with this.

change

the problem is that when it pops up, it should only start showing another view controller. if I swiped to the middle of the screen with my finger while still touching the screen, then it should show 2 view controllers. I can achieve this using push / pop like this.

enter image description here

+6
source share
2 answers

You can achieve this using Auto-layout and Swipe Gesture. The hard part sets the limits for your view. Add a negative height constraint to your view so that it doesn't display.

@IBOutlet weak var yourViewBottomConstraint: NSLayoutConstraint! //Create IBOutlet of bottom Contraint to YourView let swipeUp = UISwipeGestureRecognizer() // Swipe Up gesture recognizer let swipeDown = UISwipeGestureRecognizer() // Swipe Down gesture recognizer OR You can use single Swipe Gesture 

Than in your view DidLoad ()

 Override func viewDidLoad() { // Swipe Gesture swipeUp.direction = UISwipeGestureRecognizerDirection.up swipeUp.addTarget(self, action: "swipedViewUp") drawerButton.addGestureRecognizer(swipeUp) // Or assign to view swipeDown.direction = UISwipeGestureRecognizerDirection.down swipeDown.addTarget(self, action: "swipedViewDown") drawerButton.addGestureRecognizer(swipeDown) // Or assign to view } 

And view scroll methods

  // Toggle Swipe Action for imagesContainer func swipedViewUp(){ self.yourViewBottomConstraint.constant = +90 // Or set whatever value print("Swiped Up") } func swipedViewDown(){ self.yourViewBottomConstraint.constant = -90 // Or Set whatever value print("Swiped Down") } 
+1
source

First you need to add the UIPanGestureRecognizer to your question panel so that you can pan it to display the question view.

To manage multiple view controllers, you can use the container view controller:

 var pendingViewController: UIViewController? { didSet { if let pending = pendingViewController { addChildViewController(pending) pending.didMoveToParentViewController(self) pending.view.frame.origin.y = UIScreen.mainScreen().bounds.height view.addSubview(pending.view) } } } var currentViewController: UIViewController? { didSet { pendingViewController = nil } } func showQuestions(recognizer: UIPanGestureRecognizer) { if recognizer.state == .Began { let controller = QuestionViewController() // create instance of your question view controller pendingViewController = controller } if recognizer.state == .Changed { let translation = recognizer.translationInView(view) // Insert code here to move whatever you want to move together with the question view controller view pendingViewController.view.center.y += translation.y recognizer.setTranslation(CGPointZero, inView: view) } if recognizer.state == .Ended { // Animate the view to it location } } 

Something like that. All this is entered manually, so there may be some errors.

0
source

All Articles