广告:系统方法监听横竖屏,前提是要开启了横竖屏才可以用。iPad上可能有点用。
方法1,别人封装的一个导航,跳转vc时套上即可横竖屏切换ChangeOrientation
详细介绍
方法2,自己动手实现,切换播放器时的例子。
关闭横竖屏- (void)viewDidLoad {
[super viewDidLoad];
//监听横竖屏
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
//监听横竖屏切换的通知
- (void)deviceOrientationDidChange
{
NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
//往左侧,全屏
[self orientationChange:NO];
//旋转屏幕改变播放器frame(如果是约束,这部可以省了。)
self.playerView.frame=CGRectMake(0, 0, self.view.bounds.size.width, 200);
} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
//往右侧,恢复原始大小
[self orientationChange:YES];
//旋转屏幕改变播放器frame(如果是约束,这部可以省了。)
self.playerView.frame=self.view.bounds;
}
}
#pragma mark - 手动旋转当前view
- (void)orientationChange:(BOOL)landscapeRight
{
if (landscapeRight) {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
} else {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
}
}
/*
关闭自动旋转
在设备出于横屏时,界面就会变成横屏;
设备处于竖屏时,界面就会变成竖屏。
*/
- (BOOL)shouldAutorotate
{
return NO;
}
/*
移除通知,防止内存泄漏。
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
网友评论