如何通过Objective-C中的按钮控制自动旋转?

Wun*_*Wun 2 autorotate ios

我在发展 Objective-C

我使用以下代码锁定screen orientation并将其设置为UIInterfaceOrientationPortrait.

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

但我想screen rotate通过UIButton. 例如,如果电流shouldAutorotateNoYES当我单击 时,它将更改为Button。就像下面的伪代码。

- (IBAction) lock_Auto_rotate:(id)sender {

    if(Autorotate == YES){
       Autorotate = NO;
    }else{
       Autorotate = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何控制auto rotate由一个Button

提前致谢。

iLe*_*ner 5

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

- (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.
Run Code Online (Sandbox Code Playgroud)