问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/47024439/snapshotviewafterscreenupdates-true-returns-an-empty-view