更改rootViewController时添加动画

当需要更换Window的rootViewController时,怎么给这个过程添加动画呢?

1
2
3
4
5
6
7
[UIView transitionWithView:[[UIApplication sharedApplication].delegate window]
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[[[UIApplication sharedApplication].delegate window] setRootViewController:viewController];
}
completion:NULL];

这样写咋看咋对, 试试也是对的, 但当横屏时会发现动画很奇怪, 后来在stackoverflow看到一个解决办法

1
2
3
4
5
6
7
8
9
10
[UIView transitionWithView:[[UIApplication sharedApplication].delegate window]
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[[[UIApplication sharedApplication].delegate window] setRootViewController:viewController];
[UIView setAnimationsEnabled:oldState];
}
completion:NULL];
文章目录