こんにちは皆さん、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: {})
カスタムトランジションの画面から端からスワイプできるようにするために、NavigationControllerTransition を呼び出すカスタム NavigationController クラスがあります。
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 は閉じたりフリーズしたりしません。任意のヒント ?:(