1

我想为我的阅读器应用程序制作一个自定义方向锁定按钮,我认为鞭打它不会太糟糕,但可惜我是那个被鞭打的人。

首先,我确实有这种方法:

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

然后我在想我可以用这样的动作方法处理实际的锁定:

- (IBAction) screenLock:(id)sender{

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait){

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}else{

            [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

  }

但是,唉,这段代码不会影响指示视图旋转的前者......

我对这一切都错了吗?有什么更好的方法呢?我只想有一个本地的、简单的方法让我的用户锁定他们的屏幕方向。我想这将使用一个布尔值,他们点击一个按钮锁定,然后再次点击解锁......

想法?谢谢!!

4

1 回答 1

3

shouldAutorotateToInterfaceOrientation涟漪您的视图层次结构,因此您的逻辑需要放入您的应用程序委托(或作为可能返回 YES 的最高级 ViewController)。在您的 appDelegate 中放置一个 BOOL 属性并通过您的锁定按钮(例如目标指针/委托(AppDelegate))进行设置,然后在您的 appDelegate 中执行以下操作:

#define ROTATION_MASTER_ENABLED 1

//Setting MASTER_ROTATION_LOCK_ENABLED to 0 will stop the device rotating
//Landscape UP>landscape DOWN and Portrait UP>Portrait DOWN, 
//This is not generally desired or app store safe, default = 1

-(BOOL)compareOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    UIInterfaceOrientation actual = [[UIDevice currentDevice] orientation]; 
    if(UIInterfaceOrientationIsLandscape(interfaceOrientation) && UIInterfaceOrientationIsLandscape(actual))return YES; 
    else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)&& UIInterfaceOrientationIsPortrait(actual))return YES;
    else return NO;   

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    if(!MASTER_ROTATION_LOCK_ENABLED)return NO;
    else if(self.rotationEnabled || [self compareOrientation:interfaceOrientation])return YES;
    return NO;//self.rotationEnabled is a BOOL
}
于 2010-12-07T16:28:19.910 回答