一.问题分析
问题的原因在这篇已经说明:【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();
网友评论