简体   繁体   English

iOS 9中的自定义UIViewController过渡错误

[英]Custom UIViewController transition bug in iOS 9

I encountered a strange bug. 我遇到了一个奇怪的错误。 I am just using iOS's custom transitioning method for UIViewControllers using UIViewControllerTransitioningDelegate together with an implementation of UIViewControllerAnimatedTransitioning . 我只是使用的是iOS的自定义转换法UIViewControllers使用UIViewControllerTransitioningDelegate与实现共同UIViewControllerAnimatedTransitioning It all seems to work fine, until I do exactly the following: 一切似乎都很好,直到我完全执行以下操作:

  1. open the app 开启应用程式
  2. present another view controller with my custom transition 用我的自定义过渡呈现另一个视图控制器
  3. rotate to landscape 旋转到风景
  4. dismiss the just presented view controller 解散刚刚显示的视图控制器

That's all! 就这样! What happens now is the following: I see a large black bar on the right side of the initial view controller (as if that controller's view wasn't rotated to landscape). 现在发生的情况如下:我在初始视图控制器的右侧看到一个大的黑条(好像该控制器的视图没有旋转到横向)。

The funny thing is this only goes wrong in iOS 9, in iOS 8 everything seems to work just fine. 有趣的是,这仅在iOS 9中出错,在iOS 8中,一切似乎都正常。 Did anything change with custom transition API I don't know of? 我不知道的自定义过渡API有什么变化吗? Or is this simply a really nasty iOS 9 bug? 还是这仅仅是一个非常讨厌的iOS 9错误? If anyone can tell me what I did wrong or if anyone can provide me with a workaround I would really appreciate that! 如果有人可以告诉我我做错了什么,或者有人可以为我提供解决方法,我将不胜感激!

These classes reproduce the problem: 这些类重现该问题:

import UIKit
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tap() {
        let controller = ModalViewController()
        controller.transitioningDelegate = self
        presentViewController(controller, animated: true, completion: nil)
    }

    func animationControllerForPresentedController(presented: UIViewController,
                                                   presentingController presenting: UIViewController,
                                                   sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Transitioning()
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Transitioning()
    }
}

The presented view controller: 呈现的视图控制器:

import UIKit
class ModalViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.redColor()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tap() {
        dismissViewControllerAnimated(true, completion: nil)
    }
}

And finally the UIViewControllerAnimatedTransitioning implementation: 最后是UIViewControllerAnimatedTransitioning实现:

import UIKit
class Transitioning: NSObject, UIViewControllerAnimatedTransitioning {

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

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        let containerView = transitionContext.containerView()
        if let fromView = fromView, toView = toView {
            containerView?.addSubview(fromView)
            containerView?.addSubview(toView)

            toView.alpha = 0
            UIView.animateWithDuration(0.5, animations: {
                toView.alpha = 1
            }, completion: {
                finished in
                transitionContext.completeTransition(true)
            })
        }
    }
}

I generally use the following in animateTransition : 我通常在animateTransition使用以下animateTransition

toView.frame = fromView.frame

FYI, you don't have to add fromView to the hierarchy, as it's already there. 仅供参考,您不必将fromView添加到层次结构中,因为它已经存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM