gpt4 book ai didi

iOS 重置设备方向

转载 作者:行者123 更新时间:2023-11-28 23:29:16 25 4
gpt4 key购买 nike

考虑以下情况:iPhone 处于纵向模式。我这样做:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];

并将我的设备方向锁定为横向模式。所以我可以向任何方向转动手机,方向保持不变。太棒了;

然后我像这样解锁方向:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];

并且设备方向将根据需要遵循 iPhone 的物理方向。

现在问题只出现在这种情况下:我将方向设置为横向 (UIInterfaceOrientationLandscapeRight) 并保持手机垂直(纵向模式)。屏幕根据需要旋转。然后我在不移动手机的情况下将方向设置回 UIInterfaceOrientationUnknown...屏幕保持横向,直到我水平转动手机然后回到垂直位置。所以我需要告诉 iPhone,除了解锁方向,将屏幕方向更新为设备物理方向(例如在 Android 中自动发生)。这可能吗?最后:这至少发生在 iPhone 模拟器上。我目前没有可用于测试的真实设备。

最佳答案

您需要订阅方向更改,并在需要时恢复方向。这是完整的示例:

@interface ViewController ()
@property (nonatomic, assign) BOOL fixedOrientation;
@property (nonatomic, assign) UIDeviceOrientation lastOrientation;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.fixedOrientation = false;
[UIDevice.currentDevice beginGeneratingDeviceOrientationNotifications];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(deviceOrientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)deviceOrientationChanged:(NSNotification *)notif {
self.lastOrientation = [UIDevice.currentDevice orientation];
}

- (IBAction)buttPressed {
UIDeviceOrientation last = [UIDevice.currentDevice orientation];

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
self.fixedOrientation = true;

self.lastOrientation = last;
}

- (IBAction)butt2Pressed:(id)sender {
self.fixedOrientation = false;
NSNumber *value = [NSNumber numberWithInt:self.lastOrientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (self.fixedOrientation) {
return UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskAll;
}


@end

编辑: iPhone 和 iPad 均应选择 需要全屏 选项才能正常工作,否则 deviceOrientationChanged 将永远不会被调用。

关于iOS 重置设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57373828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com