gpt4 book ai didi

ios - 如何根据开关按钮打开和关闭来更改设备的方向?

转载 作者:可可西里 更新时间:2023-11-01 06:13:47 25 4
gpt4 key购买 nike

我需要在应用程序运行时更改应用程序方向。我的过程是,如果我打开开关,应用程序应该改变它的方向(即)(i)如果应用程序以横向运行,那么如果我打开“ON”开关按钮,那么设备方向应该自动改变为纵向模式。 . (ii) 如果应用程序以纵向模式运行,如果我关闭开关,则设备方向应自动更改为横向模式...我已经尝试了一些但它对我来说并不适用..任何人都可以提供一些解决方案为此..

 - (IBAction)PortraitSwitchPressed:(UISwitch *)sender
{
if (sender.isOn)
{
UIInterfaceOrientationMaskPortrait;
[[UIDevice currentDevice]setOrientation:UIInterfaceOrientationPortrait]; // no visible @interface for 'UIDevice' declares the selector setorientation
[self.PortraitSwitch setOn:YES animated:YES];
}
else
{

UIInterfaceOrientationMaskLandscape;
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationMaskLandscape];
[self.PortraitSwitch setOn:NO animated:YES];
}}

最佳答案

试试下面的代码:

- (IBAction)PortraitSwitchPressed:(UISwitch *)sender
{
if (sender.isOn)
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.PortraitSwitch setOn:YES animated:YES];
}
else
{

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];//or UIInterfaceOrientationLandscapeRight
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.PortraitSwitch setOn:NO animated:YES];
}
}

根据您的要求对 UIInterfaceOrientation 进行修改。

关于ios - 如何根据开关按钮打开和关闭来更改设备的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36741129/

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