跳转自动横屏

iOS 知识小集(横竖屏切换)

转载自 http://www.cocoachina.com/ios/20160722/17148.html

iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结。

注意


横屏两种情况是反的你知道吗?

UIInterfaceOrientationLandscapeRight与UIInterfaceOrientationMaskLandscapeRight都代表横屏,Home键在右侧的情况;UIDeviceOrientationLandscapeLeft则是Home键在左侧。

一般情形

所有界面都支持横竖屏切换

如果App的所有切面都要支持横竖屏的切换,那只需要勾选【General】 中的【Device Orientation】,选择希望支持的方向即可。

blob.png

图中支持竖屏和Home在右侧

如上设置完之后,当设备竖屏的时候,所有的界面都是竖屏显示的;而当设备横屏Home在右侧时,所有的界面会横屏显示。其他方向不支持,界面不会改变。

这里有个坑:

在iOS 9 之后横屏时,状态栏会消失。

解决方法:确保plist 中的【View controller-based status bar appearance】为YES,然后重写ViewController的 - (BOOL)prefersStatusBarHidden ,返回值是NO。

- (BOOL)prefersStatusBarHidden

{

return NO;

}

特殊情形

个别界面固定方向,其他所有界面都支持横竖屏切换

这种情况,在【General】-->【Device Orientation】中设置好支持的方向后,只需要在这些特殊的视图控制器中重写两个方法:

// 支持设备自动旋转

- (BOOL)shouldAutorotate

{

return YES;

}

/**

*  设置特殊的界面支持的方向,这里特殊界面只支持Home在右侧的情况

*/

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskLandscapeRight;

}

个别界面支持横竖屏切换,其他所有界面都固定方向

可能大多数App会是这种需求,某些特殊界面只能横屏,如视频播放类App。

这里有两种处理方式:

方式一

在【General】-->【Device Orientation】中设置好需要支持的所有方向。然后使用一个基类控制器,在基类控制器中重写两个控制横竖屏的方法:

// 支持设备自动旋转

- (BOOL)shouldAutorotate

{

return YES;

}

// 支持竖屏显示

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskPortrait;

}

再然后,特殊的界面上再重写这俩方法,让其可以自动切换方向。

// 如果需要横屏的时候,一定要重写这个方法并返回NO

- (BOOL)prefersStatusBarHidden

{

return NO;

}

// 支持设备自动旋转

- (BOOL)shouldAutorotate

{

return YES;

}

// 支持横屏显示

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

// 如果该界面需要支持横竖屏切换

return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;

// 如果该界面仅支持横屏

// return UIInterfaceOrientationMaskLandscapeRight;

}

方式二

用方式一的方法,还需要借助一个基类,所有的控制器都要继承这个基类,太麻烦?

另一种方式,是借助通知来控制界面的横竖屏切换。

还是整个App中大部分界面都是竖屏,某个界面可以横竖屏切换的情况。

首先,在【General】-->【Device Orientation】设置仅支持竖屏,like this:

blob.png

Device Orientation

然后在特殊的视图控制器里的ViewDidLoad中注册通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

通知方法的实现过程:

- (void)deviceOrientationDidChange

{

NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

[self orientationChange:NO];

//注意: UIDeviceOrientationLandscapeLeft 与 UIInterfaceOrientationLandscapeRight

} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

[self orientationChange:YES];

}

}

- (void)orientationChange:(BOOL)landscapeRight

{

if (landscapeRight) {

[UIView animateWithDuration:0.2f animations:^{

self.view.transform = CGAffineTransformMakeRotation(M_PI_2);

self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

}];

} else {

[UIView animateWithDuration:0.2f animations:^{

self.view.transform = CGAffineTransformMakeRotation(0);

self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

}];

}

}

// 用到的两个宏:

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

最重要的一点:

需要重写如下方法,并且返回NO。

- (BOOL)shouldAutorotate

{

return NO;

}

这样,在设备出于横屏时,界面就会变成横屏,设备处于竖屏时,界面就会变成竖屏。

填坑

上面方式二,因为【General】-->【Device Orientation】因为只设置了竖屏,所以当横屏时,如果有键盘弹出,键盘是竖屏时的样式。

解决办法:在【General】-->【Device Orientation】中加上横屏时的方向。

如果VieController 是放在UINavigationController或者UITabBarController中,需要重写它们的方向控制方法。

// UINavigationController:

- (BOOL)shouldAutorotate

{

return [self.topViewController shouldAutorotate];

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return [self.topViewController supportedInterfaceOrientations];

}

// UITabBarController:

- (BOOL)shouldAutorotate

{

return [self.selectedViewController shouldAutorotate];

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

return [self.selectedViewController supportedInterfaceOrientations];

}

如果想要点击某个按钮之后,强制将竖屏显示的界面变成横屏呢?

有人可能会想到这样写:

// 横屏

- (IBAction)landscapAction:(id)sender {

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

[self orientationChange:YES];

}

但是按照上面的写法,会导致返回到之前的界面时,视图方向错误,即使返回前执行如下代码:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

[self orientationChange:NO];

也没有作用,下面是在开源工程中无意看到的写法:

// 横屏

- (IBAction)landscapAction:(id)sender {

[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];

}

// 竖屏

- (IBAction)portraitAction:(id)sender {

[self interfaceOrientation:UIInterfaceOrientationPortrait];

}

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation

{

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

SEL selector            = NSSelectorFromString(@"setOrientation:");

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

[invocation setSelector:selector];

[invocation setTarget:[UIDevice currentDevice]];

int val                  = orientation;

[invocation setArgument:&val atIndex:2];

[invocation invoke];

}

}

上面的方法会将设备的方向强制设置为某个方向,然后再监控设备方向改变的通知,即可实现横竖屏切换。

这里有一个用JS 和原生item 控制横竖屏切换的Demo。地址

这是效果图:

727768-9d329256ad34abb8.gif

横竖屏切换.gif

横竖屏切换总结就到这来了,Have Fun!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容