gpt4 book ai didi

ios - 如何在 Objective-C 中通过 Button 控制自动旋转?

转载 作者:行者123 更新时间:2023-12-01 18:14:04 24 4
gpt4 key购买 nike

我在 Objective-C 中开发

我使用以下代码锁定屏幕方向并将其设置为UIInterfaceOrientationPortrait

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

但我想通过UIButton 动态锁定屏幕旋转。例如,如果当前 shouldAutorotateNo。当我单击 Button 时,它将更改为 YES。就像下面的伪代码。

- (IBAction) lock_Auto_rotate:(id)sender {

if(Autorotate == YES){
Autorotate = NO;
}else{
Autorotate = YES;
}
}

如何通过Button控制自动旋转

提前致谢。

最佳答案

你在正确的轨道上,只需要做一些操作。看看下面的代码片段。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isRotateOn)
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}

- (BOOL)shouldAutorotate
{
if (isRotateOn)
{
return YES;
}
else
{
return NO;
}
}

- (NSUInteger)supportedInterfaceOrientations
{

if (isRotateOn)
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}

}

-(IBAction)btnRotateOnOffAction:(id)sender
{

if ([self.btnRotateOnOff.titleLabel.text isEqualToString:@"On"])
{
isRotateOn =YES;
[self.btnRotateOnOff setTitle:@"Off" forState:UIControlStateNormal];

}
else
{
isRotateOn = NO;
[self.btnRotateOnOff setTitle:@"On" forState:UIControlStateNormal];


}

}


Let me know if you have any queries.

关于ios - 如何在 Objective-C 中通过 Button 控制自动旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24544676/

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