滑动以在Swift中弹出View Controller

时间:2018-07-02 08:47:00

标签: swift uinavigationcontroller cocoapods navigationcontroller

我在项目中使用Slide menu controller窗格,但遇到了一些问题。当导航堆栈中有控制器并且我想向左滑动时,将显示汉堡菜单,而不是弹出控制器。怎么改变呢?有人可以指导我吗?

因此,我知道这是一项非常基本的功能,您可以免费获得,但此处已对其进行了覆盖。

我尝试关闭幻灯片菜单的平移手势,但是它关闭了整个机制。

SlideMenuOptions.panGesturesEnabled = false

我还发现了handleLeftPanGesture(_ panGesture: UIPanGestureRecognizer)方法,如下所示:

open func isTargetViewController() -> Bool {
    // Function to determine the target ViewController
    // Please to override it if necessary
    guard let navController = navigationController else { return true }
    return navController.viewControllers.isEmpty
}

@objc func handleLeftPanGesture(_ panGesture: UIPanGestureRecognizer) {

    if !isTargetViewController() {
        navigationController?.popViewController(animated: true)
    }

    if isRightOpen() {
        return
    }

    switch panGesture.state {
        case UIGestureRecognizerState.began:
            if LeftPanState.lastState != .ended &&  LeftPanState.lastState != .cancelled &&  LeftPanState.lastState != .failed {
                return
            }

            if isLeftHidden() {
                self.delegate?.leftWillOpen?()
            } else {
                self.delegate?.leftWillClose?()
            }

            LeftPanState.frameAtStartOfPan = leftContainerView.frame
            LeftPanState.startPointOfPan = panGesture.location(in: view)
            LeftPanState.wasOpenAtStartOfPan = isLeftOpen()
            LeftPanState.wasHiddenAtStartOfPan = isLeftHidden()

            leftViewController?.beginAppearanceTransition(LeftPanState.wasHiddenAtStartOfPan, animated: true)
            addShadowToView(leftContainerView)
            setOpenWindowLevel()
        case UIGestureRecognizerState.changed:
            if LeftPanState.lastState != .began && LeftPanState.lastState != .changed {
                return
            }

            let translation: CGPoint = panGesture.translation(in: panGesture.view!)
            leftContainerView.frame = applyLeftTranslation(translation, toFrame: LeftPanState.frameAtStartOfPan)
            applyLeftOpacity()
            applyLeftContentViewScale()
        case UIGestureRecognizerState.ended, UIGestureRecognizerState.cancelled:
            if LeftPanState.lastState != .changed {
                setCloseWindowLevel()
                return
            }

            let velocity:CGPoint = panGesture.velocity(in: panGesture.view)
            let panInfo: PanInfo = panLeftResultInfoForVelocity(velocity)

            if panInfo.action == .open {
                if !LeftPanState.wasHiddenAtStartOfPan {
                    leftViewController?.beginAppearanceTransition(true, animated: true)
                }
                openLeftWithVelocity(panInfo.velocity)

                track(.leftFlickOpen)
            } else {
                if LeftPanState.wasHiddenAtStartOfPan {
                    leftViewController?.beginAppearanceTransition(false, animated: true)
                }
                closeLeftWithVelocity(panInfo.velocity)
                setCloseWindowLevel()

                track(.leftFlickClose)

            }
        case UIGestureRecognizerState.failed, UIGestureRecognizerState.possible:
            break
    }

    LeftPanState.lastState = panGesture.state
}

我正在尝试计算navigaion ViewController,但这没有帮助。

1 个答案:

答案 0 :(得分:2)

实际上,我找到了可能对某人有用的解决方案。

您需要将其添加到ViewController并遵守UIGestureRecognizerDelegate协议

override func viewDidAppear(_ animated: Bool) {
    slideMenuController()?.removeLeftGestures()
    navigationController?.interactivePopGestureRecognizer?.delegate = self
}

extension AdditionalInformationController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer.isEqual(navigationController?.interactivePopGestureRecognizer) {
            navigationController?.popViewController(animated: true)
        }
        return false
    }
}