简体   繁体   English

swift:在第一个视图控制器中向上滑动显示另一个视图控制器

[英]swift : show another view controller on swipe up in first view controller

Hi I checked many questions regarding swiping in SO but have doubts . 嗨我检查了很多关于在SO中滑动的问题,但有疑问。

In my app I have two pages 1. user view controller 2. question view controller 在我的应用程序中,我有两个页面1.用户视图控制器2.问题视图控制器

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. 我是Ios的新手,所以帮助我实现这个目标。

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 如果我用我的手指仍然触摸屏幕滑到屏幕中间,那么它应该显示2个视图控制器。我用这样的推/弹来实现这个

在此输入图像描述

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() 比你的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. 首先,您必须在“问题栏”中添加UIPanGestureRecognizer ,以便平移它以显示问题视图。

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. 这些都是手动输入的,因此可能会出现一些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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