【问题标题】:Custom push transition isn't working whereas the same custom present transition is自定义推送转换不起作用,而相同的自定义当前转换是
【发布时间】:2016-08-22 11:39:01
【问题描述】:

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

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

它应该在弹出时将推送视图的快照从 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)
        })
    }
}

然后,我尝试使用两种方式展示一个新的视图控制器:呈现和推送。

我的FromViewController 是UINavigationControllerDelegate 和UIViewControllerTransitioningDelegate 的子类。

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

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

我不知道如何调试这个......你有什么想法吗?

谢谢!!

【问题讨论】:

  • 你是否尝试过在 dispatchAsync 和主队列的帮助下推送它?
  • 我做了,没有帮助。我认为问题出在我创建的快照上。

标签: ios swift custom-transition


【解决方案1】:

我通过用toVC 的CGAffineTransform 替换快照元素(导致问题)找到了答案。

代码与here几乎相同。

【讨论】:

    猜你喜欢
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2012-09-10
    • 2021-04-29
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多