[英]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.