1. 在AppDelegate.h文件中 声明一个变量,
@property (nonatomic, assign) NSInteger rotateDirection;
2. 在AppDelegate.m文件中 加一个判断旋转函数
//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.rotateDirection == 1)
{
return UIInterfaceOrientationMaskLandscapeRight; // 支持右屏旋转
}
return UIInterfaceOrientationMaskPortrait;
}
3.在需要横屏的界面,横屏的点击方法里面
AppDelegate *appdelegate = ((AppDelegate *)[[UIApplication sharedApplication] delegate]);
//竖屏->横屏
if (appdelegate.rotateDirection == 0)
{
appdelegate.rotateDirection = 1;
if ([UIDevice currentDevice].orientation != 1)
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight]
forKey:@"orientation"];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
self.playerView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight); // 顺便改下播放器的UI
}
else
{
//横屏------->竖屏
appdelegate.rotateDirection = 0;
if ([UIDevice currentDevice].orientation != 4)
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight]
forKey:@"orientation"];
}
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
self.playerView.frame = CGRectMake(0, 20, ScreenWidth, ScreenWidth / 16 * 9);// 顺便改下播放器的UI
}
}