gpt4 book ai didi

ios - UINavigationController 有两个代表?

转载 作者:行者123 更新时间:2023-11-28 08:00:20 26 4
gpt4 key购买 nike

假设我有一个 UINavigationController,我想在其上执行自定义转换。为了方便起见,我定义了一个过渡类:

class WTPTransition: NSObject
{
// This is our Interaction Controller, which conforms to UIViewControllerInteractiveTransitioning. Apple's concrete class suits our needs.
var interactionController : UIPercentDrivenInteractiveTransition?
}

extension WTPTransition : UIViewControllerAnimatedTransitioning
{
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toView = transitionContext.viewController(forKey: .to)!.view!
let fromView = transitionContext.viewController(forKey: .from)!.view!

transitionContext.containerView.insertSubview(toView, belowSubview: fromView)

UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
fromView.alpha = 0.0
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}

然后我扩展它以便我可以简单地设置 UINavigationController 的 delegate 属性并获得我的转换:

extension WTPTransition : UINavigationControllerDelegate
{
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}
}

哪个工作得很好,但如果我想让另一个类观察同一个导航 Controller 的事件会怎样?我不能有两个对象作为它的委托(delegate),那么正确的模式是什么?将转换逻辑之类的东西与观察 Controller 的显示或关闭分离似乎很自然。

最佳答案

我认为这是非常肮脏的架构。

  1. 动画是仅在转换处于事件状态时才需要的对象。
  2. 您应该能够更改某些 View Controller 的动画。

我认为您可以创建委托(delegate)类,它会创建出现和消失的动画。

关于ios - UINavigationController 有两个代表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com