7

Hi I checked many questions regarding swiping in SO but have doubts .

In my app I have two pages 1. user view controller 2. question view controller

user page looks like this userpage

now what i want to implement is to show questions view controller while swiping up the users screen from bottom.

I am new to Ios, so help me in achieving this.

edit:

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

enter image description here

varun aaruru
  • 2,770
  • 1
  • 16
  • 36
  • Hi, as per you question, try to add a swipe gesture recogniser with a direction on your view & on swipe try to push/pop new screen. – Gagan_iOS Jan 27 '16 at 10:43
  • @Gagan_iOS hi thanks for the reply, the problem is while swiping up only it should start showing the other view controller. if i swiped till middle of the screen with my finger still touching the screen, then it should show 2 view controllers.can I achieve this using push/pop – varun aaruru Jan 27 '16 at 10:49
  • Sounds more like you need a `UIPanGestureRecognizer`. – Eendje Jan 27 '16 at 10:51
  • I am able to detect swipe actions,problem is showing 2 view controllers at a time – varun aaruru Jan 27 '16 at 10:54
  • No, you can't achieve this using push / pop. Easiest would be using a container view and `UIPanGestureRecognizer`. – Eendje Jan 27 '16 at 10:59
  • @varunaaruru did you find a solution to this problem? – Munib Nov 17 '16 at 05:50
  • Hey varun aaruru,I came across a similar requirement what you have,did you find any proper solution for the same? – Naresh Reddy M Nov 17 '16 at 06:29
  • Take a Look at this Repo: (https://github.com/alastor09/DraggableViewControllerDemo) – soan saini Jan 21 '19 at 22:11

2 Answers2

1

You can achieve this using Auto-layout and Swipe Gesture. Tricky part is setting constraints to your view. Add a negative of height constant constraint to your view so that it does not show in view.

@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 viewDidLoad()

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 methods to swipe view

 // 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")
}
Saqib Omer
  • 4,917
  • 7
  • 46
  • 65
0

First you'll have to add a UIPanGestureRecognizer to your "questions bar" so you can pan it to show the questions view.

To handle multiple view controllers, you can use a 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's location
    }
}

Something like this. This is all typed manually so there might be some mistakes.

Eendje
  • 8,395
  • 1
  • 26
  • 31