2010-12-07 10 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波紋アップあなたのビュー階層を、あなたのロジックが( YESを返すことがありますか、ほとんどのシニアのViewControllerなど)アプリのデリゲートに入れする必要がありますので。あなたのappDelegateでBOOLプロパティを入れて、あなたのロックボタン(例えば、ターゲットポインタ/ delegates(AppDelegate))を介してそれを設定し、その後、あなたのappDelegateにこのような何か:このメソッドは、サポートされている方向にYESを

#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または常にNOを返します。 – Eiko

+0

@Eiko、現在書かれている方法では、ロックが有効になっている場合、ビューコントローラが任意の向きに回転し(ロックが有効でない場合はYESを返します)、まったく回転しないようにします。なぜ私はこれが下垂体に値するのか分からないのですか? – Jasarien

+0

私は修正を編集しましたが、Eikoは正しかったです。 –

関連する問題