gpt4 book ai didi

ios - 自定义 UIViewController 转换的 UIView 快照

转载 作者:行者123 更新时间:2023-12-01 16:08:57 25 4
gpt4 key购买 nike

我正在为我的应用程序创建一个自定义过渡,但是当我尝试创建目标 View 的快照时,它总是显示为一个空白的白色矩形。重要的是,我注意到这是一个自定义的 push 转换,而不是 View 的模态呈现。以模态方式呈现时,快照似乎有效。这是自定义 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)
})
}

最佳答案

在 View 呈现之前对其进行快照,例如在调用 containerView.addSubview(toViewController.view) 之前进行转换的情况,根据 UIView's documentation 返回一个空白 View .

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

关于ios - 自定义 UIViewController 转换的 UIView 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com