gpt4 book ai didi

ios - 自定义推送过渡不起作用,而相同的自定义呈现过渡有效

转载 作者:行者123 更新时间:2023-11-28 08:29:40 25 4
gpt4 key购买 nike

我关注了this tutorial并观看了the WWDC video关于这个主题,但我找不到答案。

我的代码中有几乎相同的转换。当我将其作为呈现 View 而不是推送 View 时,它工作得很好。

它应该将推送 View 的快照从 CGRect 动画化到全屏,反之亦然。

这是我的 UIViewControllerAnimatedTransitioning 类的代码:

class ZoomingTransitionController: NSObject, UIViewControllerAnimatedTransitioning {

let originFrame: CGRect
let isDismissing: Bool

init(originFrame: CGRect, isDismissing: Bool) {
self.originFrame = originFrame
self.isDismissing = isDismissing
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return Constant.Animation.VeryShort
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView

guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else {
return
}

let finalFrame = transitionContext.finalFrame(for: toVC)

toVC.view.frame = finalFrame

let snapshot = self.isDismissing ? fromVC.view.snapshotView(afterScreenUpdates: true) : toVC.view.snapshotView(afterScreenUpdates: true)
snapshot?.frame = self.isDismissing ? finalFrame : self.originFrame
snapshot?.layer.cornerRadius = Constant.FakeButton.CornerRadius
snapshot?.layer.masksToBounds = true

containerView.addSubview(toVC.view)
containerView.addSubview(snapshot!)

if self.isDismissing {
fromVC.view.isHidden = true
} else {
toVC.view.isHidden = true
}

let duration = transitionDuration(using: transitionContext)

UIView.animate(withDuration: duration,
animations: {
snapshot?.frame = self.isDismissing ? self.originFrame : finalFrame
},
completion: { _ in
if self.isDismissing {
fromVC.view.isHidden = false
} else {
toVC.view.isHidden = false
}

snapshot?.removeFromSuperview()
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}

然后,我尝试使用两种方式展示一个新的 View Controller :呈现它和推送它。

我的 FromViewControllerUINavigationControllerDelegateUIViewControllerTransitioningDelegate 的子类。

FromViewController呈现 ToViewController(工作正常):

func buttonAction(_ sender: AnyObject) {
self.tappedButtonFrame = sender.frame

let toVC = self.storyboard!.instantiateViewController(withIdentifier: "ToViewController")
toVC.transitioningDelegate = self

self.present(toVC, animated: true, completion: nil)
}

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let transitionController = ZoomingTransitionController(originFrame: self.tappedButtonFrame, isDismissing: false)

return transitionController
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let transitionController = ZoomingTransitionController(originFrame: self.tappedButtonFrame, isDismissing: true)

return transitionController
}

FromViewController推送 ToViewController(不起作用):

func buttonAction(_ sender: AnyObject) {
self.tappedButtonFrame = sender.frame

let toVC = self.storyboard!.instantiateViewController(withIdentifier: "ToViewController")

self.navigationController?.pushViewController(toVC, animated: true)
}

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
switch operation {
case .push:
return ZoomingTransitionController(originFrame: self.tappedButtonFrame, isDismissing: false)

case .pop:
return ZoomingTransitionController(originFrame: self.tappedButtonFrame, isDismissing: true)

default:
return nil
}
}

推送 时,委托(delegate)方法被调用并且 ZoomingTransitionController 正常执行其代码(进入 animateTransition 直到结束没有明显问题)。但是在屏幕上,任何时候都不会显示快照 View 。 ToVC 出现在过渡持续时间之后,但同时没有任何其他内容。

我完全不知道如何调试它...你有什么想法吗?

谢谢!!

最佳答案

我通过将快照元素(导致问题的原因)替换为 toVCCGAffineTransform 找到了答案。

代码与here 几乎相同.

关于ios - 自定义推送过渡不起作用,而相同的自定义呈现过渡有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39078571/

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