5

I want to implement a custom push-transition for a navigation controller.

I first implemented the UINavigationControllerDelegate where I return an Object of UIViewControllerAnimatedTransitioning.

In this class, I want to take a snapshot of the destination view in order to animate it. However, for the push-segue, this doesn't work - the snapshot ist empty. [when I pop back, I set afterScreenUpdates = false and everything works]

    guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else {
            return
    }

    guard let snapshot = toVC.view.snapshotView(afterScreenUpdates: true) else {
        return }

   // Problem:
   // snapshot is an empty view!
Pascal
  • 2,590
  • 3
  • 21
  • 46

2 Answers2

0

Supplying true for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.

SergStav
  • 750
  • 5
  • 19
  • „*it means it needs a trip back to the runloop to actually draw the image*“ - do you have a hint how to achieve this? – Pascal Oct 31 '17 at 15:14
  • @Pascal you can achieve this using true, but if you want to see image immediately you should use false for -snapshotViewAfterScreenUpdates: – SergStav Oct 31 '17 at 17:59
0

I find that it is necessary to introduce a short delay, say about a tenth of a second, after the push transition and before taking the screenshot, in order to avoid the blank screenshot. Probably this lets the interface settle down in some way.

matt
  • 515,959
  • 87
  • 875
  • 1,141