简体   繁体   中英

swift : show another view controller on swipe up in first view controller

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

在此输入图像描述

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")
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM