繁体   English   中英

自定义 UIViewController 转换上的 UIView 快照

[英]UIView snapshots on custom UIViewController transitions

我正在为我的应用程序创建自定义过渡,但是当我尝试创建目标视图的快照时,它始终显示为空白的白色矩形。 重要的是,我注意到这是一个自定义的push转换,而不是视图的模态呈现。 当以模态呈现时,快照似乎有效。 这是自定义push / pop转换的正常行为吗?

我写的代码如下:

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)
    })
}

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

如果当前视图尚未呈现,可能是因为它尚未出现在屏幕上,则快照视图没有可见内容。

暂无
暂无

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

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