2010-12-07 63 views
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]; 

} 

    } 

但是,唉,这代码将不会摇摆在指示视图旋转的前者...

我是否在谈论这一切都是错误的?什么是更好的方法来做到这一点?我只想让本地,简单的方法让我的用户锁定屏幕的方向。我想这将使用布尔值,他们点击一个按钮锁定,然后再次打开解锁...

想法? 谢谢!

回答

3

shouldAutorotateToInterfaceOrientation涟漪您的视图层次让你的逻辑需要投入应用程式委派(或最高级的ViewController是威力返回YES)。将一个BOOL属性在你的appDelegate并通过您的锁定按钮进行设置(例如,目标指针/ 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 
} 
+1

号此方法应返回YES为支撑取向 - 在你的情况下,它总是返回YES或始终NO。 – Eiko 2010-12-07 16:31:30