作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 UINavigationController 堆栈中有一个用于推送和弹出事件的自定义动画转换,我只想将该自定义动画应用于特定的推送事件——例如,在推送 RandomViewController44() 或弹出 RandomViewController27() 时——不是每个在堆栈中推送和弹出。这是在 UINavigationControllerDelegate 中、在动画对象中还是在推送/弹出点完成的?
代表
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)
}
最佳答案
您可以在下面的委托(delegate)方法中检查 fromView 和 ToView Controller 以执行操作
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
}
}
}
当此方法根据您的要求作为设置条件调用时,您需要检查 from 和 toView Controller
关于ios - 如何将 UINavigationControllerDelegate 中的自定义过渡动画应用于特定的推送和弹出事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910210/
我是一名优秀的程序员,十分优秀!