本人在做视频全屏播放的项目中遇到的问题,应用整体是不支持横屏的。因为全屏使用的是触发手机横屏的机制,所以其他页面需要关闭屏幕自适应旋转,视频页面则开启屏幕的自适应旋转。实现代码如下:
在AppDelegate.m 中实现下面方法
- (void)initializeTheNotificationCenter
{
//在进入需要全屏的界面里面发送允许横屏的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullScreen) name:kStartFullScreen object:nil];//允许横屏
//在退出允许横屏的界面里面发送退出横屏的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:kendFullScreen object:nil];//退出横屏
}
#pragma mark 允许横屏
-(void)startFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
#pragma mark 退出横屏
-(void)endFullScreen
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//强制归正:
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];
}
}
#pragma mark 禁止横屏
- (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
记得在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中注册监听
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
[self initializeTheNotificationCenter];
...
然后在需要旋转的页面发送通知,退出页面发送关闭旋转通知
[[NSNotificationCenter defaultCenter] postNotificationName:kStartFullScreen object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kendFullScreen object:nil];