我正在为我的应用创建自定义转换,但是当我尝试创建目标视图的快照时,它始终显示为空白的白色矩形。重要的是我注意到这是一个自定义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)
})
}
答案 0 :(得分:1)
在呈现视图之前拍摄视图的快照,例如在调用containerView.addSubview(toViewController.view)
之前转换的情况,按UIView's documentation返回空白视图。
如果当前视图尚未呈现,可能是因为它尚未在屏幕上显示,则快照视图没有可见内容。