简体   繁体   中英

UIView snapshots on custom UIViewController transitions

I am creating a custom transition for my app but when I try to create a snapshot of the destination view it always appears as a blank white rectangle. It is important I note that this is a custom push transition and not a modal presentation of the view. When presenting modally the snapshot appears to work. Is this the normal behaviour for custom push / pop transitions?

The code I've written is as follows:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let toViewController = transitionContext.viewController(forKey: .to) as? CultureViewController else {
        return
    }

    let containerView = transitionContext.containerView
    let finalFrame = transitionContext.finalFrame(for: toViewController)

    let snapshot = toViewController.view.snapshotView(afterScreenUpdates: true)
    snapshot?.frame = UIScreen.main.bounds
    containerView.addSubview(snapshot!)

    toViewController.view.transform = CGAffineTransform(translationX: 0, y: toViewController.view.bounds.height)
    //containerView.addSubview(toViewController.view)

    let duration = transitionDuration(using: transitionContext)
    UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeCubic, animations: { 
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: { 
            toViewController.view.frame = finalFrame
        })
    }, completion: { _ in
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    })
}

Taking a snapshot of a view before it is rendered, such as is the case with a transition before you call containerView.addSubview(toViewController.view) , returns a blank view, per UIView's documentation .

If the current view is not yet rendered, perhaps because it is not yet onscreen, the snapshot view has no visible content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM