有時(shí)我們的APP并沒有適配橫屏的需求,但是在個(gè)別視頻播放界面,我們需要在播放視頻的時(shí)候橫屏,退出全屏的時(shí)候不能橫屏,但是有時(shí)候并沒有原生API并沒有給出解決方案。
當(dāng)其他界面不支持橫屏?xí)r:
這個(gè)解決方法比較容易
在 APPDelegate.h
文件中增加屬性:是否支持橫屏
/*** 是否允許橫屏的標(biāo)記 */
@property (nonatomic,assign)BOOL allowRotation;
在 APPDelegate.m
文件中增加方法,控制全部不支持橫屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
這樣在其他界面想要橫屏的時(shí)候,我們只要控制 allowRotation
這個(gè)屬性就可以控制其他界面進(jìn)行橫屏了。
//需在上面#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
//不讓橫屏的時(shí)候 appDelegate.allowRotation = NO;即可
播放界面橫屏
所以這里可以使用 UIWindow
的通知,就可以解決
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//進(jìn)入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
在退出全屏?xí)r,增加邏輯讓其強(qiáng)制編程豎屏,這樣當(dāng)全屏播放的時(shí)候,點(diǎn)擊 down("完成")
時(shí),就會(huì)自動(dòng)變成豎屏了。
// 進(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];
}
}
上面的兩個(gè)方案已經(jīng)足夠解決只有部分界面需要支持橫屏的問題了,親測(cè)可用