Ios 如何将UINavigationControllerDelegate中的自定义转换动画应用于特定的推送和弹出事件?
在UINavigationController堆栈中,我有一个用于推送和弹出事件的自定义动画转换,我只想将该自定义动画应用于特定的推送事件——例如,在推送RandomViewController44()或弹出RandomViewController27()时——而不是堆栈中的每个推送和弹出事件。这是在UINavigationControllerDelegate、animator对象中还是在推/弹出点完成的?Ios 如何将UINavigationControllerDelegate中的自定义转换动画应用于特定的推送和弹出事件?,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,在UINavigationController堆栈中,我有一个用于推送和弹出事件的自定义动画转换,我只想将该自定义动画应用于特定的推送事件——例如,在推送RandomViewController44()或弹出RandomViewController27()时——而不是堆栈中的每个推送和弹出事件。这是在UINavigationControllerDelegate、animator对象中还是在推/弹出点完成的? 代表 extension BaseViewController: UINavigati
代表
extension BaseViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
switch operation {
case .push:
return InAnimator()
case .pop:
return OutAnimator()
default:
return nil
}
}
}
动画师对象
class InAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let animationDuration = 0.5
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return animationDuration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
containerView.addSubview(toViewController!.view)
toViewController!.view.frame = CGRect(x: screenWidth * -1, y: 0, width: screenWidth, height: screenHeight)
UIView.animate(withDuration: animationDuration, animations: {
toViewController!.view.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
}, completion: { finished in
let cancelled = transitionContext.transitionWasCancelled
transitionContext.completeTransition(!cancelled)
})
}
}
呼叫推送
func pushVC() {
navigationController?.pushViewController(randomViewController44(), animated: true)
}
您可以在下面的委托方法中检查fromView和ToView控制器以执行操作
extension BaseViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
switch operation {
case .push:
if toVc is RandomViewController44{
return InAnimator()
}
return nil
case .pop:
if RandomViewController27 is fromVC{
return OutAnimator()
}
return nil
default:
return nil
}
}
}
根据您的要求,当此方法调用为set condition时,您需要检查from和toView控制器