1.基于导航的试图控制器翻转,需要展示导航栏,因为不支持状态栏旋转,所以屏蔽了状态栏
以下代码写在需要单独翻转的VC中。
- (void)viewDidLoad {
[super viewDidLoad];
//以下代码用于隐藏状态栏
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]
}
}
- (BOOL)prefersStatusBarHidden {
return YES;//隐藏为YES,显示为NO
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
//视图将要显示时 翻转
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//时间
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);//翻转角度
self.navigationController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
[UIView commitAnimations];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
//视图将要消失的时候 再把视图翻转回来 ,不会影响其他VC的展示
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*2);
self.navigationController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[UIView commitAnimations];
}
2. 不需要展示导航栏的,或者因为需求想要自定义导航栏的 ,只需要在 viewWillAppear 方法里面加上 隐藏导航栏的代码
self.navigationController.navigationBar.hidden = YES;
在 viewWillAppear 方法的翻转动画的代码中 改成对view做翻转就行,如下:
self.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
self.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);
然后在视图将要消失的时候将导航栏的hidden再改为NO,注意 因为只是对当前vc的view做翻转,所以不会影响导航控制器,也就是当前页面的翻转不会影响到其他页面,
所以在viewWillDisappear里面 不需要再对view做旋转了。