有時我們的APP并沒有適配橫屏的需求付呕,但是在個別視頻播放界面,我們需要在播放視頻的時候橫屏跌捆,退出全屏的時候不能橫屏徽职,但是有時候并沒有原生API并沒有給出解決方案。
當(dāng)其他界面不支持橫屏?xí)r:
這個解決方法比較容易
在 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;
}
這樣在其他界面想要橫屏的時候姆钉,我們只要控制 allowRotation
這個屬性就可以控制其他界面進(jìn)行橫屏了。
//需在上面#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
//不讓橫屏的時候 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)全屏播放的時候钙姊,點(diǎn)擊 down("完成")
時毯辅,就會自動變成豎屏了。
// 進(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];
}
}
上面的兩個方案已經(jīng)足夠解決只有部分界面需要支持橫屏的問題了煞额,親測可用