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

时间:2016-01-27 10:40:41

标签: ios iphone swift uiswipegesturerecognizer

您好我检查了许多关于在SO中滑动的问题,但有疑问。

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

用户页面如下所示 userpage

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

我是Ios的新手,所以帮助我实现这个目标。

编辑:

问题是在向上滑动时它应该开始显示另一个视图控制器。如果我用手指仍然触摸屏幕直到屏幕中间滑动,那么它应该显示2个视图控制器。我用这样的推/弹来实现这个

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

首先,您必须在“问题栏”中添加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
    }
}

像这样的东西。这些都是手动输入的,因此可能会出现一些错误。