自定义水平弹出过渡无法正常工作

Posted

技术标签:

【中文标题】自定义水平弹出过渡无法正常工作【英文标题】:Custom horizontal pop transition not working as it should 【发布时间】:2017-08-11 14:49:47 【问题描述】:

我正在尝试创建自定义水平幻灯片 UINavigationController 过渡。并且推送动画似乎工作正常。但是当我试图弹出(并让水平滑动回来)只有一个空白屏幕并且在动画时间之后视图出现在它应该出现的位置时

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 

        let container = transitionContext.containerView
        let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

        let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
        let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)

        fromView.frame = transitionContext.initialFrame(for: transitionContext.viewController(forKey: .from)!)
        toView.frame = transitionContext.finalFrame(for: transitionContext.viewController(forKey: .to)!)

        toView.transform = isPresenting == true ? offScreenRight : offScreenLeft



        container.addSubview(toView)
        container.addSubview(fromView)

        let duration = self.transitionDuration(using: transitionContext)

        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: 

            fromView.transform = self.isPresenting == true ? offScreenLeft : offScreenRight
            toView.transform = .identity

        , completion:  finished in

            toView.frame = transitionContext.initialFrame(for: transitionContext.viewController(forKey: .from)!)
            transitionContext.completeTransition(true)
        )
    

【问题讨论】:

【参考方案1】:

您似乎需要重置视图上的所有现有转换。

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 

    let container = transitionContext.containerView
    let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
    let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

    // add these two lines      
    fromView.transform = .identity
    toView.transform = .identity

    // ... the rest of your existing code

【讨论】:

是的,你是对的。做到了!谢谢!我认为这些观点从一开始就很干净,呵呵:)

以上是关于自定义水平弹出过渡无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

iOS 自定义弹出过渡 - 就像在 snapchat 故事上向下滑动

如何将 UINavigationControllerDelegate 中的自定义过渡动画应用于特定的推送和弹出事件?

如何使用自定义 UIViewController 转换来实现这一点?

在 UINavigationController 中定义自定义过渡动画器时锚定键盘,如 iMessage 应用程序

自定义 Snackbar 无法正常工作

导航控制器中的两个不同的自定义转换