swift : show another view controller on swipe up in first view controller
Asked Answered
Q

2

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

Queenqueena answered 27/1, 2016 at 10:40 Comment(8)
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.Oligarchy
@Oligarchy 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/popQueenqueena
Sounds more like you need a UIPanGestureRecognizer.Dragging
I am able to detect swipe actions,problem is showing 2 view controllers at a timeQueenqueena
No, you can't achieve this using push / pop. Easiest would be using a container view and UIPanGestureRecognizer.Dragging
@varunaaruru did you find a solution to this problem?Oppenheimer
Hey varun aaruru,I came across a similar requirement what you have,did you find any proper solution for the same?Denni
Take a Look at this Repo: (github.com/alastor09/DraggableViewControllerDemo)Coppage
H
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")
}
Hexateuch answered 27/1, 2016 at 11:15 Comment(0)
D
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.

Dragging answered 27/1, 2016 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.