gpt4 book ai didi

ios - UIViewControllerAnimatedTransitioning 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 13:30:43 24 4
gpt4 key购买 nike

我想在navigationController的推送和弹出上制作一个过渡动画。但它的工作原理真的很奇怪。就像 1/5 倍一样,fromViewController 会淡出,但 toViewController 不会淡入,它只是出现。限制有时也会变得很奇怪。如果我使用标准过渡,那就没问题了。这是我的过渡类(class):

class TMFadeAnimator: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
toViewController?.beginAppearanceTransition(true, animated: true)
fromViewController?.beginAppearanceTransition(false, animated: true)

transitionContext.containerView()!.addSubview(toViewController!.view)


toViewController!.view.alpha = 0.0

UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
toViewController!.view.alpha = 1.0
fromViewController!.view.alpha = 0.0
}) { (finished) in
toViewController?.endAppearanceTransition()
fromViewController?.endAppearanceTransition()
transitionContext.completeTransition(finished)
}
}
}

最佳答案

奇怪的是,通过将 toViewController?.view.frame = fromViewController!.view.frame 放在 toViewControllerfromViewController 后面就解决了问题> 被定义。 (感谢 A'sa Dickens 的发现)。这是最终的代码

class TMFadeAnimator: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)

toViewController?.view.frame = fromViewController!.view.frame

toViewController?.beginAppearanceTransition(true, animated: true)
fromViewController?.beginAppearanceTransition(false, animated: true)

transitionContext.containerView()!.addSubview(toViewController!.view)


toViewController!.view.alpha = 0.0

UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
toViewController!.view.alpha = 1.0
fromViewController!.view.alpha = 0.0
}) { (finished) in
toViewController?.endAppearanceTransition()
fromViewController?.endAppearanceTransition()
transitionContext.completeTransition(finished)
}
}
}

关于ios - UIViewControllerAnimatedTransitioning 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600276/

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