六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 141|回复: 0

Code Snippets for iOS Device Orientation

[复制链接]

升级  84%

56

主题

56

主题

56

主题

秀才

Rank: 2

积分
176
 楼主| 发表于 2012-12-19 22:18:06 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">从iOS Code Snippets看来的技术,挺方便的,转载记录于此。
在XCode4中,项目属性设置中很容易就可以配置iOS项目支持设备持有方向,如图:
可惜,这个设置仅仅是在plist中存储了相关设置,真正要控制某个UIView的设备翻转支持,你还得在相关的UIViewController中折腾-shouldAutorotateToInterfaceOrientation:函数,根据不同的设备持有方向,来返回YES或NO。
这个code snippet简化了相关操作,通过它你可以直接在shouldAutorotateToInterfaceOrientation:函数中查询plist的相关设置,根据设置来进行返回,而不用手工代码来进行一一判断。
<div class="cnblogs_code">static inline NSString *    NSStringFromUIInterfaceOriention(UIInterfaceOrientation orientation){    switch (orientation) {        case UIInterfaceOrientationPortrait:            return @"UIInterfaceOrientationPortrait";        case UIInterfaceOrientationPortraitUpsideDown:            return  @"UIInterfaceOrientationPortraitUpsideDown";        case UIInterfaceOrientationLandscapeLeft:            return @"UIInterfaceOrientationLandscapeLeft";        case UIInterfaceOrientationLandscapeRight:            return @"UIInterfaceOrientationLandscapeRight";        default:            return @"Unexpected";    }}static inline BOOL UIInterfaceOrientationIsSupportedOrientation(UIInterfaceOrientation interfaceOrientation){    NSArray *array = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];    NSUInteger index = [array indexOfObject:NSStringFromUIInterfaceOriention(interfaceOrientation)];    return index != NSNotFound;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表