大家好,我在关闭 NavigationController 时遇到问题。这段代码对我不起作用。当我尝试在我的 ViewController 中调用它时
@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
这就是我在我的第一个 ViewController 中所做的,我只需打开具有自定义转换的 ProfileViewController。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Profile") as? NavigationControllerTransition
controller?.modalPresentationStyle = .custom
controller?.transitioningDelegate = self
controller?.interactor = self.interactor
controller?.animatorDirection.direction = .right
self.animatedTransition.animatorDirection.direction = .right
self.present(controller!, animated: true, completion: {})
我有一个自定义 NavigationController 类,它调用 NavigationControllerTransition 以使其碰巧从屏幕边缘滑动以进行自定义转换。
class NavigationControllerTransition: UINavigationController {
var interactor: Interactor?
var animatorDirection = AnimatorDirection()
override func viewDidLoad() {
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(NavigationControllerTransition.handleTap(sender:)))
self.view.addGestureRecognizer(panGesture)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func handleTap(sender: UIPanGestureRecognizer) {
let percentThreshold: CGFloat = 0.3
let translation = sender.translation(in: view)
var horitonzalMovement: CGFloat = 0.0
var downwardMovementPercent: Float = 0.0
if self.animatorDirection.direction == .left {
horitonzalMovement = -(translation.x / view.bounds.width)
let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
downwardMovementPercent = fminf(downwardMovement, 1.0)
} else {
horitonzalMovement = +(translation.x / view.bounds.width)
let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
downwardMovementPercent = fminf(downwardMovement, 1.0)
}
let progress = CGFloat(downwardMovementPercent)
guard let interactor = interactor else { return }
switch sender.state {
case .began:
interactor.hasStarted = true
dismiss(animated: true, completion: nil)
case .changed:
interactor.shouldFinish = progress > percentThreshold
interactor.update(progress)
case .cancelled:
interactor.hasStarted = false
interactor.cancel()
case .ended:
interactor.hasStarted = false
interactor.shouldFinish ? interactor.finish() : interactor.cancel()
default: break
}
}
}
所以我可以滑动来打开或关闭我的 ViewController 并且过渡效果很好。唯一的问题是当我尝试使用 UIButton 关闭 ViewController 时。NavigationController 不会关闭和冻结。有小费吗 ?:(