swift - 如何禁用仅在一个 View Controller 上专门向后滑动

标签 swift uigesturerecognizer swipe

我知道有人问过类似的问题,但没有一个对我有用。

我有这段代码可以在我的项目中启用向后滑动

    class InteractivePopRecognizer: NSObject {

    // MARK: - Properties

    fileprivate weak var navigationController: UINavigationController?

    // MARK: - Init

    init(controller: UINavigationController) {
        self.navigationController = controller

        super.init()

        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}

extension InteractivePopRecognizer: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return (navigationController?.viewControllers.count ?? 0) > 1
    }

    // This is necessary because without it, subviews of your top controller can cancel out your gesture recognizer on the edge.
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

我有这个 VC 堆栈

HomescreenVC -> Login/SignupVC -> UserProfileVC

我不希望他们能够从 UserProfileVC 向后滑动。

最佳答案

更好的方法是在显示 UserProfileVC

时将它们从堆栈中清除
let profile  = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]

编辑:在 profileVC 中执行此操作

self.navigationController?.viewControllers = [self]

//

self.view.alpha = 0

UIView.animate(withDuration: 0.5) {

    self.view.alpha = 1

}

关于swift - 如何禁用仅在一个 View Controller 上专门向后滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631162/

相关文章:

c# - Unity 滑动功能问题

ios - 如果我将 subview 添加到 UINavigationBar,默认后退按钮将停止工作

swift - 带有预设正文的 WatchKit SMS

swift - 手势识别器不工作。执行下面元素的操作 - SWIFT

ios - 如何手动取消 MKMapView 标准手势识别器

Android 滑动布局以关闭

ios - 屏幕上显示的应用内购买价格(带货币)

swift - 单元格内容未显示在 UICollectionView 上

objective-c - 如何将手势识别器添加到一系列 subview 中?

java - 点击 Android 滑动 fragment