美文网首页
【React Native】react-native-orien

【React Native】react-native-orien

作者: 嗖嗖编程 | 来源:发表于2018-10-22 10:02 被阅读0次

    一.问题分析

    问题的原因在这篇已经说明:【iOS】当设备方向已经旋转,转屏效果失效的解决方案

    二.解决方案

    1.修改 react-native-orientation 的iOS工程下的 Orientation.m 文件

    RCT_EXPORT_METHOD(lockToPortrait)
    {
        #if DEBUG
            NSLog(@"Locked to Portrait");
        #endif
        [Orientation setOrientation:UIInterfaceOrientationMaskPortrait];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    -   //    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    +   [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
      }];
    }
    
    
    RCT_EXPORT_METHOD(lockToLandscape)
    {
        #if DEBUG
            NSLog(@"Locked to Landscape");
        #endif
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        NSString *orientationStr = [self getSpecificOrientationStr:orientation];
        if ([orientationStr isEqualToString:@"LANDSCAPE-LEFT"]) {
            [Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    -           //  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    +           [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
                [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
            }];
        } else {
            [Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    -           //  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    +           [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
                [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
            }];
        }
    }   
    

    2.调用转屏方法

    // 横屏
    Orientation.lockToLandscape();
    
    // 转屏
    Orientation.lockToPortrait();
    

    相关文章

      网友评论

          本文标题:【React Native】react-native-orien

          本文链接:https://www.haomeiwen.com/subject/zmpwzftx.html