如何进行自定义定位锁定操作?

时间:2010-12-07 16:21:26

标签: iphone locking action orientation autorotate

我想为我的读者应用程序制作一个自定义方向锁定按钮,我觉得鞭打不会太糟糕,但唉,我是那个被鞭打的人。

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

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

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

- (IBAction) screenLock:(id)sender{

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

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}else{

            [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

  }

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

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

思考? 谢谢!

1 个答案:

答案 0 :(得分:3)

shouldAutorotateToInterfaceOrientation掀起了您的视图层次结构,因此您的逻辑需要放入您的app委托(或者作为可能返回YES的最高级ViewController)。在你的appDelegate中放置一个BOOL属性并通过你的锁定按钮设置它(例如目标指针/ delegates(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
}