Bladeren bron

音视频对设备旋转的支持

heavyrain2012 4 jaren geleden
bovenliggende
commit
8172ef6b67

+ 27 - 22
wfuikit/WFChatUIKit/Voip/WFCUFloatingWindow.m

@@ -51,8 +51,8 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
     [self updateWindow];
     [self registerTelephonyEvent];
     [[NSNotificationCenter defaultCenter] addObserver:self
-                                             selector:@selector(onOrientationChanged:)
-                                                 name:UIApplicationDidChangeStatusBarOrientationNotification
+                                             selector:@selector(onDeviceOrientationDidChange:)
+                                                 name:UIDeviceOrientationDidChangeNotification
                                                object:nil];
     [self addProximityMonitoringObserver];
 }
@@ -67,7 +67,7 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
     };
 }
 
-- (void)onOrientationChanged:(NSNotification *)notification {
+- (void)onDeviceOrientationDidChange:(NSNotification *)notification {
     [self updateWindow];
 }
 
@@ -101,6 +101,23 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
     }
 }
 
+//1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
+- (BOOL)shouldAutorotate {
+      return YES;
+}
+
+//2.返回支持的旋转方向
+//iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
+//iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
+- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
+     return UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight | UIDeviceOrientationPortrait;
+}
+
+//3.返回进入界面默认显示方向
+- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
+     return UIInterfaceOrientationPortrait;
+}
+
 - (void)updateWindow {
     CGFloat posX = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosX] floatValue];
     CGFloat posY = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosY] floatValue];
@@ -110,16 +127,14 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
     posX = (posX + 30) > screenBounds.size.width ? (screenBounds.size.width - 30) : posX;
     posY = (posY + 48) > screenBounds.size.height ? (screenBounds.size.height - 48) : posY;
 
-    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft &&
-        [self isSupportOrientation:UIInterfaceOrientationLandscapeLeft]) {
+    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
         self.window.transform = CGAffineTransformMakeRotation(M_PI / 2);
         self.window.frame = CGRectMake(posX, posY, 64, 96);
         self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
         if ([self isVideoViewEnabledSession]) {
             self.videoView.frame = CGRectMake(0, 0, 96, 64);
         }
-    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight &&
-               [self isSupportOrientation:UIInterfaceOrientationLandscapeRight]) {
+    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
         self.window.transform = CGAffineTransformMakeRotation(-M_PI / 2);
         self.window.frame = CGRectMake(posX, posY, 64, 96);
         self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
@@ -127,8 +142,7 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
             self.videoView.frame = CGRectMake(0, 0, 96, 64);
         }
     } else {
-        if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown &&
-            [self isSupportOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
+        if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
             self.window.transform = CGAffineTransformMakeRotation(M_PI);
         } else {
             self.window.transform = CGAffineTransformMakeRotation(0);
@@ -240,28 +254,19 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
     return _floatingButton;
 }
 
-- (BOOL)isSupportOrientation:(UIInterfaceOrientation)orientation {
-    UIInterfaceOrientationMask mask =
-        [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.window];
-    return mask & (1 << orientation);
-}
-
 - (void)handlePanGestures:(UIPanGestureRecognizer *)paramSender {
     if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed) {
         CGPoint location = [paramSender locationInView:[UIApplication sharedApplication].windows[0]];
 
-        if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft &&
-            [self isSupportOrientation:UIInterfaceOrientationLandscapeLeft]) {
+        if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
             CGFloat tmp = location.x;
             location.x = [UIScreen mainScreen].bounds.size.height - location.y;
             location.y = tmp;
-        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight &&
-                   [self isSupportOrientation:UIInterfaceOrientationLandscapeRight]) {
+        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
             CGFloat tmp = location.x;
             location.x = location.y;
             location.y = [UIScreen mainScreen].bounds.size.width - tmp;
-        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown &&
-                   [self isSupportOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
+        } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
             CGFloat tmp = location.x;
             location.x = [UIScreen mainScreen].bounds.size.height - location.y;
             location.y = [UIScreen mainScreen].bounds.size.width - tmp;
@@ -280,7 +285,7 @@ static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
 
         CGRect screenBounds = [UIScreen mainScreen].bounds;
         BOOL isLandscape = screenBounds.size.width > screenBounds.size.height;
-        if (isLandscape && [self isSupportOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation]) {
+        if (isLandscape) {
             if (frame.origin.y + frame.size.height > [UIScreen mainScreen].bounds.size.width) {
                 frame.origin.y = [UIScreen mainScreen].bounds.size.width - 2 - frame.size.height;
             }

+ 84 - 0
wfuikit/WFChatUIKit/Voip/WFCUMultiVideoViewController.m

@@ -206,6 +206,14 @@
     [self updateTopViewFrame];
     
     [self didChangeState:self.currentSession.state];//update ui
+    
+    
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(onDeviceOrientationDidChange)
+                                                 name:UIDeviceOrientationDidChangeNotification
+                                               object:nil];
+    [self onDeviceOrientationDidChange];
+
 }
 
 - (UIButton *)hangupButton {
@@ -483,6 +491,72 @@
     }
 }
 
+//1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
+- (BOOL)shouldAutorotate {
+      return YES;
+}
+
+//2.返回支持的旋转方向
+//iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
+//iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
+- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
+     return UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight | UIDeviceOrientationPortrait;
+}
+
+//3.返回进入界面默认显示方向
+- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
+     return UIInterfaceOrientationPortrait;
+}
+
+- (BOOL)onDeviceOrientationDidChange{
+    //获取当前设备Device
+    UIDevice *device = [UIDevice currentDevice] ;
+
+    switch (device.orientation) {
+        case UIDeviceOrientationFaceUp:
+            NSLog(@"屏幕幕朝上平躺");
+            break;
+
+        case UIDeviceOrientationFaceDown:
+            NSLog(@"屏幕朝下平躺");
+            break;
+
+        case UIDeviceOrientationUnknown:
+            //系统当前无法识别设备朝向,可能是倾斜
+            NSLog(@"未知方向");
+            break;
+
+        case UIDeviceOrientationLandscapeLeft:
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(M_PI_2);
+            NSLog(@"屏幕向左橫置");
+            break;
+
+        case UIDeviceOrientationLandscapeRight:
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(-M_PI_2);
+            NSLog(@"屏幕向右橫置");
+            break;
+
+        case UIDeviceOrientationPortrait:
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(0);
+            NSLog(@"屏幕直立");
+            break;
+
+        case UIDeviceOrientationPortraitUpsideDown:
+            NSLog(@"屏幕直立,上下顛倒");
+            break;
+
+        default:
+            NSLog(@"無法识别");
+            break;
+    }
+    
+    if (!self.smallCollectionView.hidden) {
+        [self.smallCollectionView reloadData];
+    }
+    return YES;
+}
+
+
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     
@@ -1030,6 +1104,16 @@
         WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId inGroup:self.currentSession.conversation.type == Group_Type ? self.currentSession.conversation.target : nil refresh:NO];
         
         
+        UIDevice *device = [UIDevice currentDevice] ;
+        if (device.orientation == UIDeviceOrientationLandscapeLeft) {
+            cell.transform = CGAffineTransformMakeRotation(M_PI_2);
+        } else if (device.orientation == UIDeviceOrientationLandscapeRight) {
+            cell.transform = CGAffineTransformMakeRotation(-M_PI_2);
+        } else {
+            cell.transform = CGAffineTransformMakeRotation(0);
+        }
+        
+        
         if ([userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
             WFAVParticipantProfile *profile = self.currentSession.myProfile;
             [cell setUserInfo:userInfo callProfile:profile];

+ 76 - 0
wfuikit/WFChatUIKit/Voip/WFCUVideoViewController.m

@@ -145,6 +145,12 @@
     [self updateTopViewFrame];
     
     [self didChangeState:self.currentSession.state];//update ui
+    
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(onDeviceOrientationDidChange)
+                                                 name:UIDeviceOrientationDidChangeNotification
+                                               object:nil];
+    [self onDeviceOrientationDidChange];
 }
 
 - (UIButton *)hangupButton {
@@ -733,5 +739,75 @@
         }];
     }
 }
+//1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
+- (BOOL)shouldAutorotate {
+      return YES;
+}
+
+//2.返回支持的旋转方向
+//iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
+//iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
+- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
+     return UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight | UIDeviceOrientationPortrait;
+}
+
+//3.返回进入界面默认显示方向
+- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
+     return UIInterfaceOrientationPortrait;
+}
+
+- (BOOL)onDeviceOrientationDidChange{
+    //获取当前设备Device
+    UIDevice *device = [UIDevice currentDevice] ;
+    //识别当前设备的旋转方向
+    CGRect smallVideoFrame = CGRectZero;
+    CGFloat width = self.view.bounds.size.width;
+    CGFloat height = self.view.bounds.size.height;
+    switch (device.orientation) {
+        case UIDeviceOrientationFaceUp:
+            NSLog(@"屏幕幕朝上平躺");
+            break;
+
+        case UIDeviceOrientationFaceDown:
+            NSLog(@"屏幕朝下平躺");
+            break;
+
+        case UIDeviceOrientationUnknown:
+            //系统当前无法识别设备朝向,可能是倾斜
+            NSLog(@"未知方向");
+            break;
+
+        case UIDeviceOrientationLandscapeLeft:
+            self.smallVideoView.transform = CGAffineTransformMakeRotation(M_PI_2);
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(M_PI_2);
+            smallVideoFrame = CGRectMake(width - SmallVideoView - 8, height - 8 - kStatusBarAndNavigationBarHeight + 64 - SmallVideoView - SmallVideoView/3 - kTabbarSafeBottomMargin, SmallVideoView * 4 /3, SmallVideoView);
+            NSLog(@"屏幕向左橫置");
+            break;
+
+        case UIDeviceOrientationLandscapeRight:
+            self.smallVideoView.transform = CGAffineTransformMakeRotation(-M_PI_2);
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(-M_PI_2);
+            smallVideoFrame = CGRectMake(8-SmallVideoView/3, 8 + kStatusBarAndNavigationBarHeight - 64+SmallVideoView/3, SmallVideoView * 4 /3, SmallVideoView);
+            NSLog(@"屏幕向右橫置");
+            break;
+
+        case UIDeviceOrientationPortrait:
+            self.smallVideoView.transform = CGAffineTransformMakeRotation(0);
+            self.bigVideoView.transform = CGAffineTransformMakeRotation(0);
+            smallVideoFrame = CGRectMake(self.view.frame.size.width - SmallVideoView, kStatusBarAndNavigationBarHeight, SmallVideoView, SmallVideoView * 4 /3);
+            NSLog(@"屏幕直立");
+            break;
+
+        case UIDeviceOrientationPortraitUpsideDown:
+            NSLog(@"屏幕直立,上下顛倒");
+            break;
+
+        default:
+            NSLog(@"無法识别");
+            break;
+    }
+    self.smallVideoView.frame = smallVideoFrame;
+    return YES;
+}
 #endif
 @end