监听 UIDeviceOrientationDidChangeNotification的广播,再根据[[UIDevice currentDevice] orientation]获取到屏幕的方向。
-(void) viewDidLoad { // Request to turn on accelerometer and begin receiving accelerometer events [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (void)orientationChanged:(NSNotification *)notification { // 获取当前屏幕方向interfaceOrientation UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationLandscapeLeft) {// 左横屏 [self allPlay]; } else {// 其他的都是正竖屏 [self normalPlay]; } } -(void) viewDidDisappear { // Request to stop receiving accelerometer events and turn off accelerometer [[NSNotificationCenter defaultCenter] removeObserver:self]; [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; } - (void) allPlay { // 按钮 [self.allScreenPlayBtn setHidden:YES]; [self.normalPlayBtn setHidden:NO]; // 状态栏 isStatusBaeHidden = YES; [self prefersStatusBarHidden]; // 是否隐藏状态栏 // 导航栏 [self.navigationController setNavigationBarHidden:YES animated:YES]; // view旋转 [UIView beginAnimations:@"rotate" context:nil]; [UIView setAnimationDuration:0.6f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; self.cbPlayer.view.transform = CGAffineTransformRotate(self.view.transform, M_PI_2); self.normalPlayBtn.transform = CGAffineTransformRotate(self.normalPlayBtn.transform, M_PI_2); [UIView commitAnimations]; CGSize size = [[UIScreen mainScreen] bounds].size; CGRect rect = CGRectMake(0, -20, size.width, size.height); [self.cbPlayer.view setFrame:rect]; } - (void) normalPlay { // 按钮 [self.allScreenPlayBtn setHidden:NO]; [self.normalPlayBtn setHidden:YES]; // 状态栏 isStatusBaeHidden = NO; [self prefersStatusBarHidden]; // 导航栏 [self.navigationController setNavigationBarHidden:NO animated:YES]; // view旋转 [UIView beginAnimations:@"rotate" context:nil]; [UIView setAnimationDuration:0.6f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; self.cbPlayer.view.transform = CGAffineTransformRotate(self.view.transform, 0); self.normalPlayBtn.transform = CGAffineTransformRotate(self.normalPlayBtn.transform, 0); [UIView commitAnimations]; [self.cbPlayer.view setFrame:self.playView.frame]; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。