首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在UINavigationController中禁用边缘滑动的过渡动画?

在UINavigationController中禁用边缘滑动的过渡动画可以通过自定义交互式转场来实现。下面是一种实现方法:

  1. 创建一个自定义的交互式转场类,继承自UIPercentDrivenInteractiveTransition,并遵循UIGestureRecognizerDelegate协议。
代码语言:txt
复制
import UIKit

class CustomInteractionTransition: UIPercentDrivenInteractiveTransition, UIGestureRecognizerDelegate {
    
    weak var navigationController: UINavigationController?
    var gestureRecognizer: UIScreenEdgePanGestureRecognizer?
    
    init(navigationController: UINavigationController, gestureRecognizer: UIScreenEdgePanGestureRecognizer) {
        super.init()
        
        self.navigationController = navigationController
        self.gestureRecognizer = gestureRecognizer
        self.gestureRecognizer?.addTarget(self, action: #selector(handleGesture(_:)))
        self.gestureRecognizer?.delegate = self
    }
    
    @objc private func handleGesture(_ gestureRecognizer: UIScreenEdgePanGestureRecognizer) {
        let viewTranslation = gestureRecognizer.translation(in: gestureRecognizer.view?.superview)
        let progress = viewTranslation.x / (gestureRecognizer.view?.bounds.width ?? 1)
        
        switch gestureRecognizer.state {
        case .began:
            navigationController?.popViewController(animated: true)
        case .changed:
            update(progress)
        case .cancelled, .ended:
            if progress > 0.5 {
                finish()
            } else {
                cancel()
            }
        default:
            break
        }
    }
    
    // MARK: - UIGestureRecognizerDelegate
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let navigationController = navigationController,
              let topViewController = navigationController.topViewController else {
            return false
        }
        
        return topViewController.shouldDisableEdgeSwipe
    }
}
  1. 在需要禁用边缘滑动的UIViewController中,添加一个属性来标识是否禁用边缘滑动。
代码语言:txt
复制
import UIKit

class CustomViewController: UIViewController {
    
    var shouldDisableEdgeSwipe: Bool = false
    
    // ...
}
  1. 在需要禁用边缘滑动的UIViewController的viewDidLoad方法中,创建自定义的交互式转场,并将其关联到UINavigationController上。
代码语言:txt
复制
import UIKit

class CustomViewController: UIViewController {
    
    var shouldDisableEdgeSwipe: Bool = false
    var interactionTransition: CustomInteractionTransition?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create a UIScreenEdgePanGestureRecognizer for the right edge
        let edgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleEdgePan(_:)))
        edgePanGestureRecognizer.edges = .right
        view.addGestureRecognizer(edgePanGestureRecognizer)
        
        // Create the interaction transition and associate it with the navigation controller
        if shouldDisableEdgeSwipe, let navigationController = navigationController {
            interactionTransition = CustomInteractionTransition(navigationController: navigationController, gestureRecognizer: edgePanGestureRecognizer)
        }
    }
    
    @objc private func handleEdgePan(_ gestureRecognizer: UIScreenEdgePanGestureRecognizer) {
        // Handle the gesture if needed
    }
    
    // ...
}

通过以上步骤,你可以在UINavigationController中禁用边缘滑动的过渡动画。注意,这里只是禁用了边缘滑动的过渡动画,而导航控制器其他的交互行为仍然有效。

对于推荐的腾讯云相关产品和产品介绍链接地址,由于题目要求不能提及具体的云计算品牌商,因此无法给出相应的推荐。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券