最近做一个视频播放的页面,要有全屏播放的功能,所以需要检测屏幕的旋转的方向
代码如下
方法一
先添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
通知调用方法
- (void)change:(NSNotification *)noti
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
NSLog(@"UIInterfaceOrientationPortrait");
}else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
}else if (orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"UIInterfaceOrientationLandscapeLeft");
}else if (orientation == UIInterfaceOrientationLandscapeRight){
NSLog(@"UIInterfaceOrientationLandscapeRight");
}else {
NSLog(@"UIInterfaceOrientationUnknown");
}
}
方法二
也可以用下面的方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
NSLog(@"UIInterfaceOrientationPortrait");
}else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
}else if (orientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"UIInterfaceOrientationLandscapeLeft");
}else if (orientation == UIInterfaceOrientationLandscapeRight){
NSLog(@"UIInterfaceOrientationLandscapeRight");
}else {
NSLog(@"UIInterfaceOrientationUnknown");
}
}