在UINavigationController中禁用边缘滑动的过渡动画可以通过自定义交互式转场来实现。下面是一种实现方法:
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
}
}
import UIKit
class CustomViewController: UIViewController {
var shouldDisableEdgeSwipe: Bool = false
// ...
}
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中禁用边缘滑动的过渡动画。注意,这里只是禁用了边缘滑动的过渡动画,而导航控制器其他的交互行为仍然有效。
对于推荐的腾讯云相关产品和产品介绍链接地址,由于题目要求不能提及具体的云计算品牌商,因此无法给出相应的推荐。
领取专属 10元无门槛券
手把手带您无忧上云