执行自定义search时,呼叫不平衡

我创build了一个自定义的segue,这是一个垂直segue的反转版本,这里是我的执行function:

var sourceViewController = self.sourceViewController as UIViewController! var destinationViewController = self.destinationViewController as UIViewController! sourceViewController.view.addSubview(destinationViewController.view) destinationViewController.view.frame = sourceViewController.view.frame destinationViewController.view.transform = CGAffineTransformMakeTranslation(0, -sourceViewController.view.frame.size.height) destinationViewController.view.alpha = 1.0 UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in destinationViewController.view.transform = CGAffineTransformMakeTranslation(0, 0) }) { (finished: Bool) -> Void in destinationViewController.view.removeFromSuperview() sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil) } 

当我在我的应用程序中执行它的作品和animation是我想要的,但我在控制台中有这个警告:

 Unbalanced calls to begin/end appearance transitions for <Custom_Segues.ViewController: 0x7a3f9950>. 

我在Stack Overflow上看到很多关于这个问题的post,但是我没有发现一个和我的情况有关的东西,有人知道这个问题是什么吗? 我在代码上尝试了很多东西,我知道问题出现在最后两行,但是我不知道要改变什么。

编辑/回答:

阅读答案后,我find了一个解决scheme:改变视图,然后应用旧的VC和新的animation。 代码是安全的,在animation结束时没有旧的VC的闪光。

 var sourceViewController = self.sourceViewController as UIViewController! var destinationViewController = self.destinationViewController as UIViewController! var duplicatedSourceView: UIView = sourceViewController.view.snapshotViewAfterScreenUpdates(false) // Screenshot of the old view. destinationViewController.view.addSubview(duplicatedSourceView) // We add a screenshot of the old view (Bottom) above the new one (Top), it looks like nothing changed. sourceViewController.presentViewController(destinationViewController, animated: false, completion: { destinationViewController.view.addSubview(duplicatedSourceView) // We add the old view (Bottom) above the new one (Top), it looks like nothing changed. UIView.animateWithDuration(0.33, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in duplicatedSourceView.transform = CGAffineTransformMakeTranslation(0, sourceViewController.view.frame.size.height) // We slide the old view at the bottom of the screen }) { (finished: Bool) -> Void in duplicatedSourceView.removeFromSuperview() } }) } 

这看起来runloop时间相关。

 destinationViewController.view.removeFromSuperview() sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil) 

这两行不应该在同一个runloop内执行。

 destinationViewController.view.removeFromSuperview() // Force presentViewController() into a different runloop. let time = dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC))) dispatch_after(time, dispatch_get_main_queue()) { sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil) } 

我添加了这个作为评论,但我认为这足以做出第二个解决scheme。 看了如何自定义模态视图控制器呈现animation?

解决scheme转换为swift:

 var transition = CATransition() transition.duration = 1 transition.type = kCATransitionFade transition.subtype = kCATransitionFromBottom sourceViewController.view.window?.layer.addAnimation(transition, forKey: kCATransition) sourceViewController.presentViewController(destinationViewController, animated:false, completion:nil) 

您可以调整以符合您的需求。