最近在做一個(gè)視頻實(shí)時(shí)監(jiān)控的app跋涣,用到螢石開放平臺(tái)的SDK來進(jìn)行開發(fā)。螢石的demo里面在特定頁面通過簡單的幾句代碼調(diào)用就實(shí)現(xiàn)了全屏和退出全屏的功能和二。
//進(jìn)入全屏
- (IBAction)large:(id)sender
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
//退出全屏
- (IBAction)largeBack:(id)sender
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
而我在按照螢石官方文檔一步一步將SDK導(dǎo)入我的工程,卻出現(xiàn)無法全屏的問題耳胎。
在參考了iOS 屏幕方向那點(diǎn)事兒惯吕、UIWebView 全屏播放視頻解決辦法幾篇博文之后,最后采用如下方法解決了全屏問題:
1. 全局禁止橫屏
在appdelegate.h添加以下屬性:
/*** 是否允許橫屏的標(biāo)記 */
@property (nonatomic,assign)BOOL allowRotation;
appdelegate.m添加如下代碼:
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {//如果設(shè)置了allowRotation屬性怕午,支持全屏
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;//默認(rèn)全局不支持橫屏
}
2. 在需要支持橫屏的界面改變allowRotation屬性
//進(jìn)入全屏
-(void)begainFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強(qiáng)制歸正:
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 =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
在viewWillAppear
和viewWillDisappear
分別調(diào)用以上方法废登,在該控制器下即可順利支持全屏。