快照视图在执行UiviewControllerAnimatedTransitioning时被隐藏



我在推动uiviewController时试图进行简单的动画过渡,但似乎我缺少某些东西。

我将来自FromViewController到ToviewController的子视图的快照动画。我正在动画快照的框架,但是快照在动画的整个过程中都是看不见的。

这是一个简单的代码示例。我正在尝试将单个Uilabel从第一个控制器到第二个动画。我特别想为从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中看到。

::::::用于显示查看控制器

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

:::::::::::::::::::::在推动视图控制器

时使用
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)
    }
}

相关内容

  • 没有找到相关文章