简体   繁体   English

自定义 UIViewController 转换上的 UIView 快照

[英]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.重要的是,我注意到这是一个自定义的push转换,而不是视图的模态呈现。 When presenting modally the snapshot appears to work.当以模态呈现时,快照似乎有效。 Is this the normal behaviour for custom push / pop transitions?这是自定义push / pop转换的正常行为吗?

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 .根据UIView 的文档,在渲染之前拍摄视图的快照,例如在调用containerView.addSubview(toViewController.view)之前的过渡情况,返回一个空白视图。

If the current view is not yet rendered, perhaps because it is not yet onscreen, the snapshot view has no visible content.如果当前视图尚未呈现,可能是因为它尚未出现在屏幕上,则快照视图没有可见内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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