gpt4 book ai didi

ios - 执行 UIViewControllerAnimatedTransitioning 时隐藏快照 View

转载 作者:行者123 更新时间:2023-11-29 00:03:12 24 4
gpt4 key购买 nike

我试图在推送 UIViewController 时进行简单的动画转换,但似乎我遗漏了一些东西。

我将 subview 的快照从 fromViewController 动画化到 toViewController。我正在为快照的帧设置动画,但快照在整个动画过程中都是不可见的。

这是一个简单的代码示例。我正在尝试将单个 UILabel 从第一个 Controller 动画化到第二个 Controller 。我特别想为从 toViewConroller 而不是 fromViewController 拍摄的快照制作动画。

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromVC = transitionContext.viewController(forKey: .from) as! ViewController
let toVC = transitionContext.viewController(forKey: .to) as! SecondViewController

let container = transitionContext.containerView

toVC.view.frame = fromVC.view.frame
container.addSubview(toVC.view)
toVC.view.layoutIfNeeded()

let animatedFromView = fromVC.label!
let animatedToView = toVC.label!

let initialFrame = container.convert(animatedFromView.frame,
from: animatedFromView.superview)
let finalFame = container.convert(animatedToView.frame,
to: animatedToView.superview)

let snapshot = animatedToView.snapshotView(afterScreenUpdates: true)!
snapshot.frame = initialFrame

container.addSubview(snapshot)

animatedFromView.alpha = 0
animatedToView.alpha = 0

UIView.animate(withDuration: 2,
animations: {
snapshot.frame = finalFame

}) { (_) in
snapshot.removeFromSuperview()

fromVC.label.alpha = 1
toVC.label.alpha = 1

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}

我猜快照是隐藏的,因为将 animatedToView 的 alpha 设置为 0,但是我不确定如何在不设置它的情况下实现该动画。

最佳答案

我试过你的代码,它工作正常。我改变了一些东西,比如对初始帧进行硬编码,这样我就可以看到效果,也可以从 viewController alpha 看到效果。

::::::用于呈现 View Controller

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromVC = transitionContext.viewController(forKey: .from) as! ViewController
let toVC = transitionContext.viewController(forKey: .to) as! SecondViewController

let container = transitionContext.containerView

toVC.view.frame = fromVC.view.frame
container.addSubview(toVC.view)
toVC.view.layoutIfNeeded()

let animatedFromView = fromVC.view!
let animatedToView = toVC.view!

let initialFrame = container.convert(CGRect(x: 0, y: 200, width: 100, height: 100),
from: animatedFromView.superview)
let finalFame = container.convert(animatedToView.frame,
to: animatedToView.superview)

let snapshot = animatedToView.snapshotView(afterScreenUpdates: true)!
snapshot.frame = initialFrame

container.addSubview(snapshot)

animatedFromView.alpha = 1
animatedToView.alpha = 0

UIView.animate(withDuration: 2,
animations: {
snapshot.frame = finalFame

}) { (_) in
snapshot.removeFromSuperview()

fromVC.view.alpha = 1
toVC.view.alpha = 1

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}

::::::::::::::::::在推送 View Controller 时使用

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else { return }
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else { return }

let container = transitionContext.containerView

container.insertSubview(toView, belowSubview: fromView)

let animatedFromView = fromView
let animatedToView = toView

let initialFrame = container.convert(CGRect(x: 0, y: 200, width: 100, height: 100),
from: animatedFromView.superview)

let finalFame = container.convert(animatedToView.frame,
to: animatedToView.superview)

let snapshot = animatedToView.snapshotView(afterScreenUpdates: true)!
snapshot.frame = initialFrame

container.addSubview(snapshot)

animatedFromView.alpha = 1
animatedToView.alpha = 1

UIView.animate(withDuration: 2,
animations: {
snapshot.frame = finalFame

}) { (_) in
snapshot.removeFromSuperview()

//fromVC.view.alpha = 1
//toVC.view.alpha = 1

transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}

关于ios - 执行 UIViewControllerAnimatedTransitioning 时隐藏快照 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48852559/

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