使用自定义 segue 对 begin/end 外观转换的不平衡调用

Unbalanced calls to begin/end appearance transitions with custom segue

在按下按钮时实现以下交叉溶解自定义转场(在 https://www.youtube.com/watch?v=xq9ZVsLNcWw 中学习):

override func perform() {

    var src:UIViewController = self.sourceViewController as! UIViewController
    var dstn:UIViewController = self.destinationViewController as! UIViewController

    src.view.addSubview(dstn.view)

   dstn.view.alpha = 0

    UIView.animateWithDuration(0.75 , delay: 0.1, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in


        dstn.view.alpha = 1


    }) { (finished) -> Void in

        dstn.view.removeFromSuperview()
        src.presentViewController(dstn, animated: false, completion: nil)

    }

}

我得到 "Unbalanced calls to begin/end appearance transitions" warning/error.

我已经彻底搜索了很多Whosebug问题:

Unbalanced calls to begin/end appearance transitions for <UITabBarController: 0x197870>

Keep getting "Unbalanced calls to begin/end appearance transitions for <ViewController>" error

"Unbalanced calls to begin/end appearance transitions" warning when push a view in a modal way in XCode 4 with Storyboard

"Unbalanced calls to begin/end appearance transitions for DetailViewController" when pushing more than one detail view controller

Unbalanced calls to begin/end appearance transitions for UITabBarController

然而仍然无济于事,warning/error 仍然弹出。

我知道这个错误是什么意思,它试图在加载前一个之前呈现一个新的 viewcontroller,但不确定如何解决它。

我唯一的线索是之前的(来源)viewcontroller 在最终 viewcontroller 加载之前快速弹出,如 0:04 处

所示

https://youtu.be/i1D5fbcjNjY

然而,我猜只有大约 5% 的时间出现故障。

有什么想法吗?

看起来这种行为是最近 iOS 版本中某些(到目前为止)未记录更改的结果。 对您的确切问题和缺乏令人满意的答案感到沮丧,我想出了这个:

// create a UIImageView containing a UIImage of `view`'s contents
func createMockView(view: UIView) -> UIImageView {
    UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.mainScreen().scale)

    view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    return UIImageView(image: image)
}

override func perform() {

   let src:UIViewController = self.sourceViewController as! UIViewController
   let dstn:UIViewController = self.destinationViewController as! UIViewController
   let mock = createMockView(dstn.view)

   src.view.addSubview(mock)
   mock.alpha = 0

   UIView.animateWithDuration(0.75, delay: 0.1, options: UIViewAnimationOptions.TransitionCrossDissolve, 
     animations: { () -> Void in
        mock.alpha = 1
     },
     completion: { (finished) -> Void in
        src.presentViewController(dstn, animated: false, completion: { mock.removeFromSuperView()})
   })
}

createMockView() 将创建一个 UIImageView,其中包含目标 VC 内容的快照,并使用它来制作过渡动画。 一旦转换完成,真正的目的地 VC 将在没有动画的情况下呈现,从而导致两者之间的无缝转换。最后,一旦演示完成,模拟视图就会被删除。