總所周知,在實際業務中,經常會有替換根控制器的需求 例如: 1、登入成功後,切換到tabbarController 2、展示完新特性頁後,切換到tabbarController 3、掉線後回到登入後 ... 但是直接切換rootVC的時候,介面很生硬,非常不合理 但是替換根控制器,又不是切換頁面,無法自定義轉場動畫 因此可以把動畫考慮放在UIWindow上 廢話不多說 上程式碼 OC版
UITabBarViewController *tabBar = [[UITabBarViewController alloc] init];
CATransition *transtition = [CATransition animation];
transtition.duration = 0.5;
transtition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[UIApplication sharedApplication].keyWindow.rootViewController = tabBar;
[[UIApplication sharedApplication].keyWindow.layer addAnimation:transtition forKey:@"animation"];
複製程式碼
Swift 4.0版
let tabBarController =UITabbarController()
let transtition = CATransition()
transtition.duration = 0.5
transtition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
UIApplication.shared.keyWindow?.layer.add(transtition, forKey: "animation")
UIApplication.shared.keyWindow?.rootViewController = tabBarController
複製程式碼
有了這個思路,可以實現更多炫酷的動畫,來實現替換根控制器的轉場動畫