3
votes

I use custom interactive pop transition with pan gesture recognizer. I have subclass from UIPercentDrivenInteractiveTransition for handle user actions:

class InteractiveTransitionManager: UIPercentDrivenInteractiveTransition {

  var viewController: UIViewController?
  var interactive: Bool = false

  var popInProgress = false
  var startX: CGFloat = 0

  init(viewController: UIViewController) {
    super.init()
    self.viewController = viewController
    self.viewController?.view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "handlePan:"))
  }

  func handlePan(pan: UIPanGestureRecognizer) {
    if pan.velocityInView(pan.view!.superview!).x > 0 && !popInProgress {
      popInProgress = true
      interactive = true
      startX = pan.locationInView(pan.view!.superview!).x
      viewController?.navigationController?.popViewControllerAnimated(true)
      return
    }
    let curX = pan.locationInView(pan.view!.superview!).x
    let progress: CGFloat = max(0, curX - startX) / pan.view!.frame.width
    if pan.state == .Changed {
      updateInteractiveTransition(progress)
    } else if pan.state == .Ended || pan.state == .Cancelled {
      if progress > 0.5 {
        finishInteractiveTransition()
      } else {
        cancelInteractiveTransition()
      }
      popInProgress = false
      interactive = false
    }
  }

}

And I have InteractivePopTransition class for animation of transition:

class InteractivePopTransition: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.3
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
    let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
    let containterView = transitionContext.containerView()

    let targetFrame = transitionContext.finalFrameForViewController(toVC)

    toView.frame = CGRect(x: -targetFrame.size.width, y: targetFrame.origin.y, width: targetFrame.width, height: targetFrame.height)
    containterView?.insertSubview(toView, belowSubview: fromView)

    UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
        toView.frame = targetFrame
        fromView.frame.origin.x = CGRectGetMaxX(targetFrame)
        }, completion: { b in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    })
}

}

I have a problem that completion block in UIView.animateWithDuration sometime not called. Why this happens and how to fix it?

1

1 Answers

1
votes

When animateTransition(transitionContext:) is called and the user-interaction was not interactive (like a programmatic animation trigger), there seems to be a timing issue with the system and UIView.animateWithDuration's completion is not called, like you described.

I experienced this in my work and I was able to solve the issue by explicitly calling finishInteractiveTransition on the UIPercentDrivenInteractiveTransition object from within animateTransition(transitionContext:), before calling UIView.animateWithDuration(...).

This was a simple solution for my case because my UIViewControllerAnimatedTransitioning object had an instance of the UIPercentDrivenInteractiveTransition subclass, and I exposed an interactionInProgress property to determine when I should explicitly call finishInteractiveTransition.

If you explicitly call finishInteractiveTransition on the interactive transition object then the completion block will be called.

class InteractivePopTransition: NSObject, UIViewControllerAnimatedTransitioning {
  private var interactiveTransitionManager: InteractiveTransitionManager
  ...
  func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    ...
    if !interactiveTransitionManager.isInProgress {
      // Weird state where system is telling you to animate a transition, but there
      // is no interactive transition occurring. Manually finish the non-existant
      // interactive transition so system will call other animation callbacks
      // appropriately to complete the entire transition
      interactiveTransitionManager.finish()
    }
    UIView.animateWithDuration(...)
  }  
}

Another solution is to put UIView.animateWithDuration inside a dispatch async block with a small delay.