Swift 当使用动画更改rootViewController时,ViewWillAspect/DidAspect调用两次

Swift 当使用动画更改rootViewController时,ViewWillAspect/DidAspect调用两次,swift,cocoa-touch,uiviewcontroller,Swift,Cocoa Touch,Uiviewcontroller,以下是我的动画/过渡的代码: if let window = self.window where animated { window.rootViewController?.dismissViewControllerAnimated(true, completion: { UIView.transitionFromView(window.rootViewController!.view, toView: tabBarController.view, duration: 0.8

以下是我的动画/过渡的代码:

if let window = self.window where animated {
   window.rootViewController?.dismissViewControllerAnimated(true, completion: {
       UIView.transitionFromView(window.rootViewController!.view, toView: tabBarController.view, duration: 0.8, options: .TransitionFlipFromLeft, completion: { _ in
           window.rootViewController = tabBarController
       })
   })
}
如果没有动画代码(仅设置
rootviewcontroller
),则不会出现问题


你知道为什么它会让视图出现两次吗?我该如何解决这个问题

使用
UIViewController.transitionFromViewController1
,而不是使用
UIView.transitionFromView
。像这样做:

//
//  AppDelegate.swift
//  helloapp
//
//  Created by quota on 2/18/16.
//  Copyright © 2016 liu. All rights reserved.
//

import UIKit


class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.greenColor()
        let button = UIButton()
        button.setTitle("transit", forState: .Normal)
        button.frame = CGRectMake(20, 20, 100,20)
        button.addTarget(self, action: "click:", forControlEvents: .TouchDown)
        view.addSubview(button)
    }
    func click(sender:UIButton!){
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        if let window = appDelegate.window {
//            let v2 = ViewController2()

            if let r = window.rootViewController{
                r.transitionFromViewController(
                    appDelegate.v1!
                    ,toViewController:appDelegate.v2!,duration:0.8,options: .TransitionFlipFromLeft,animations:nil){_ in }
            }
        }
    }

}


class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.blueColor()
    }
    override func viewWillAppear(animated: Bool) {
        print(animated)
    }

}

class ViewController3: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.redColor()
    }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var v1 : ViewController1?
    var v2 : ViewController2?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.rootViewController = ViewController3()
        v1 = ViewController1()
        v2 = ViewController2()
        v1!.view.frame = self.window!.frame
        self.window!.rootViewController?.addChildViewController(v1!)
        self.window!.rootViewController?.view.addSubview(v1!.view)
        self.window!.rootViewController?.addChildViewController(v2!)

        self.window?.makeKeyAndVisible()
        return true
    }
}