swift:在第一个视图控制器中显示另一个视图控制器

您好,我检查了许多关于刷卡的问题,但有疑问。

在我的应用程序,我有两个页面1.用户视图控制器2.问题视图控制器

用户页面看起来像这样 userpage

现在我想实现的是从底部向上滑动用户屏幕时显示问题视图控制器。

我是新来的Ios,所以帮助我实现这一点。

编辑:

问题是在刷卡时只能开始显示其他视图控制器。 如果我用手指仍然触摸屏幕一直滑到屏幕中间,那么它应该显示2个视图控制器。我可以使用push / pop来实现这个function

在这里输入图像说明

首先,您必须将UIPanGestureRecognizer添加到“问题栏”中,以便可以平移显示问题视图。

要处理多个视图控制器,可以使用容器视图控制器:

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

像这样的东西。 这是全部手动input,所以可能会有一些错误。

您可以使用自动布局和滑动手势来实现此目的。 棘手的部分是为您的观点设置约​​束。 将高度常数约束的负数添加到视图中,以便它不会显示在视图中。

 @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 

比你的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 } 

和滑动视图的方法

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