概述
iOS8之后的屏幕旋转和iOS6,7有很大不同,项目中自己之前遇到过这样的需求,从A界面呈现B界面,如果A横屏则呈现出的B也为横屏,如果A竖屏则呈现出的B也为竖屏,实现代码如下:
//当前屏幕高度
#define MainScreenHeight [UIScreen mainScreen].bounds.size.height
//当前屏幕宽度
#define MainScreenWidth [UIScreen mainScreen].bounds.size.width
//webView是当前满屏的控件
- (void)viewDidLoad {
[super viewDidLoad];[UIApplication sharedApplication].statusBarHidden = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
if (UIInterfaceOrientationIsLandscape(orientation)) {
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
webViewWidth = MainScreenWidth;
webViewHeight = MainScreenHeight;
} else {
webViewWidth = MainScreenHeight;
webViewHeight = MainScreenWidth;
}
} else {
webViewWidth = MainScreenWidth;
webViewHeight = MainScreenHeight;
}
}
- (BOOL)prefersStatusBarHidden{
return YES;
}
//设置是否支持旋转
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
// 如果状态栏竖着的,不支持controller的旋转
return NO;
} else if (UIInterfaceOrientationIsLandscape(orientation)) {
return YES;
}
return NO;
}
// 直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown
- (NSUInteger)supportedInterfaceOrientations {
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation)) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
在iOS8之前横屏后,屏幕的宽和高的数值是对换了(即宽变成了高,高变成了宽),
但在iOS8之后宽高并没有对换,所以在iOS8之后旋转横屏如果不对换宽高值的话,就会造成屏幕一侧为空的情况.
(代码是挺早之前写的了,可能会有些乱)
上面的代码是从A界面呈现B界面,如果A横屏,则present横屏vc,竖屏下present竖屏vc。
如果仅在当前vc进行横竖屏旋转操作,可以监听系统的通知『UIWindowWillRotateNotification』,如下:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenRotate:animation:) name:@"UIWindowWillRotateNotification" object:nil];
- ( void )screenRotate:( NSNotification *)noti animation:( BOOL )animation
{
UIInterfaceOrientation orientation = [[noti. userInfo objectForKey : @"UIWindowNewOrientationUserInfoKey" ] integerValue ];
if (!noti) {
return ;
}
animation = YES ;
NSTimeInterval i = [ UIApplication sharedApplication ]. statusBarOrientationAnimationDuration ;
NSTimeInterval time = 0.3 + i;
if (!animation) {
time = 0.0 ;
}
switch (orientation)
{
case UIInterfaceOrientationPortrait :
{
}
break ;
case UIInterfaceOrientationPortraitUpsideDown :
{
}
break ;
case UIInterfaceOrientationLandscapeRight :
{
// 这里是给相应的view对应旋转
[ UIView animateWithDuration :time animations :^{
_userVc . view . transform = CGAffineTransformMakeRotation ( M_PI_2 );
} completion : nil ];
}
break ;
case UIInterfaceOrientationLandscapeLeft :
{
// 这里是给相应的的view对应旋转
[ UIView animateWithDuration :time animations :^{
_userVc . view . transform = CGAffineTransformMakeRotation (- M_PI_2 );
} completion : nil ];
}
break ;
default :
break ;
}
}
最后
以上就是包容飞机为你收集整理的iOS屏幕横竖屏旋转相关的全部内容,希望文章能够帮你解决iOS屏幕横竖屏旋转相关所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复