【问题标题】:Swipe to pop View Controller in Swift在 Swift 中滑动弹出视图控制器
【发布时间】:2018-07-02 08:47:00
【问题描述】:

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

所以我知道这是一个非常基本的功能,你可以免费获得,但在这里它被覆盖了。

我尝试关闭滑动菜单的平移手势,但它关闭了整个机制。

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
}

我正在尝试计算导航 ViewController,但没有帮助。

【问题讨论】:

    标签: swift uinavigationcontroller cocoapods navigationcontroller


    【解决方案1】:

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

    你需要将这个添加到 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
        }
    }
    

    【讨论】:

    • 如果我们需要交互手势你怎么办
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多