UIViewControllerAnimatedTransitioning:多次调用interrupttibleAnimator?

时间:2017-06-29 20:26:05

标签: ios swift uiviewcontroller uiviewpropertyanimator

我花了最近几个小时来追踪碰巧位于过渡驱动程序对象中的错误,多次调用interruptibleAnimator方法,其中:

  1. UIViewPropertyAnimator完成闭包包含对[unowned self]的引用,以完成其转换控制器使用self.context.completeTransition(true)传递的转换上下文
  2. 转换控制器,实现UIViewControllerAnimatedTransitioning并且是UINavigationControllerDelegate,通过其animate方法传递上下文

    extension NavigationTransitionController: UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return transitionDuration
    }
    
    func animationEnded(_ transitionCompleted: Bool) {
    
        // Clean up our helper object and any additional state
        transitionDriver = nil
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        transitionDriver = TransitionDriver(context: transitionContext, duration: transitionDuration)
        interruptibleAnimator(using: transitionContext).startAnimation()
    }
    
    func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {
        return transitionDriver!.transitionAnimator
    }
    
  3. 除非我遗漏了一些非常基本的东西,否则我不知道为什么转换控制器会对interruptibleAnimator方法进行多次调用。

    我的结构受到Apple提出的here in their WWDC 2016 talk的启发;如果您下载,设置断点并启动他们的Photo Transitioning应用程序,您可以亲眼看到此方法在其情况下针对相同的转换被调用两次。

    有人可以告诉我,我是在追逐一个鬼还是这是一个合法的奇怪行为?

1 个答案:

答案 0 :(得分:2)

Apple在UIViewControllerAnimatedTransitioning的'标题文档'中声明:

/// A conforming object implements this method if the transition it creates can
/// be interrupted. For example, it could return an instance of a
/// UIViewPropertyAnimator. It is expected that this method will return the same
/// instance for the life of a transition.
@available(iOS 10.0, *)
optional public func interruptibleAnimator(using transitionContext: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating

同样在WWDC Session 2016的29:34分段陈述。在27:33分钟幻灯片上的示例实现似乎是误导/错误。

多次调用它的原因是我所不知道的。