【问题标题】:How do I handle orientation changes in the presenting UIViewController when using a custom transition使用自定义转换时如何处理呈现的 UIViewController 中的方向变化
【发布时间】:2018-07-17 02:06:13
【问题描述】:

我有一个 UIViewController 作为根视图控制器,它使用自定义转换呈现另一个 UIViewController。当您在查看子视图控制器时旋转设备,然后返回根视图控制器,根视图控制器的视图框架不会更新。我已将其范围缩小到自定义过渡的存在,即,如果您没有自定义过渡,它就可以正常工作。进行自定义转换并让根视图控制器适当地处理方向更改的最佳方法是什么?

我在这里有一个演示项目:https://github.com/rawbee/TestOrientation

【问题讨论】:

  • 问题是你写错了过渡。您对presentdismiss 使用完全相同的转换,没有任何变化——也就是说,您的转换不区分这是哪一个。那是错误的。它需要在两种不同的情况下做完全相反的事情!
  • @matt 需要什么不同,在这两种情况下,我只想淡出视图控制器以显示目标视图控制器。
  • @rawbee 你刚刚忘记为新的视图控制器设置框架,看我的回答
  • 不同之处在于to 视图控制器和from 视图控制器在两种不同的情况下是颠倒的。你没有考虑到这个事实。
  • @matt,我认为它们被反转的事实允许重新使用过渡。即目前;从 == 根(当前显示)到 == 子级,对于解雇,从 == 子级(当前显示)到 == 父级。所以在这两种情况下,过渡都会淡出当前显示的视图控制器。还是我误会了?

标签: ios iphone ipad uiviewcontroller


【解决方案1】:

似乎您只是忘记为正在呈现的视图控制器设置框架:

toVC.view.frame = fromVC.view.frame

只需将这一行添加到animateTransition,使方法看起来像这样:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromVC = transitionContext.viewController(forKey: .from),
        let toVC = transitionContext.viewController(forKey: .to),
        let snapshot = fromVC.view.snapshotView(afterScreenUpdates: false)
        else { return }

    let containerView = transitionContext.containerView

    // you want the newly presented view to have the same frame as the old one
    toVC.view.frame = fromVC.view.frame

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

    let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), curve: .easeInOut) {
        snapshot.alpha = 0.0
    }
    animator.addCompletion { _ in
        snapshot.removeFromSuperview()
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }
    animator.startAnimation()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2011-02-19
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多