2010-12-07 61 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