iOS - 淡入淡出切换rootViewController
2016-08-05 本文已影响3217人
Cdream
1、UIModalPresentationStyle(弹出风格)
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终以UIModalPresentationFullScreen模式显示。
UIModalPresentationFullScreen代表弹出VC时,VC充满全屏;
UIModalPresentationPageSheet代表弹出VC时,VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击.这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域;
UIModalPresentationFormSheet这种模式下,VC的高度和宽度均会小于屏幕尺寸,VC居中显示,四周留下变暗区域;
UIModalPresentationCurrentContext这种模式下,VC的弹出方式和弹出VC的VC的父VC的方式相同。
2、UIModalTransitionStyle(弹出时的动画风格)
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。
3、例子
// 登陆后淡入淡出更换rootViewController
- (void)restoreRootViewController:(UIViewController *)rootViewController
{
typedef void (^Animation)(void);
UIWindow* window = [UIApplication sharedApplication].keyWindow;
rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
Animation animation = ^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[UIApplication sharedApplication].keyWindow.rootViewController = rootViewController;
[UIView setAnimationsEnabled:oldState];
};
[UIView transitionWithView:window
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:animation
completion:nil];
}