2010-12-07 3 views
1

내 독자 앱에 맞춤 오리엔테이션 잠금 버튼을 만들고 싶습니다. 나는 채찍질하기에는 너무 나쁘지 않을 것이라고 생각했습니다.하지만 슬프게도 나는 휘핑을 입었습니다. 이를사용자 지정 방향 잠금 동작을 수행하려면 어떻게해야합니까?

- (IBAction) screenLock:(id)sender{ 

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

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

}else{ 

      [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

} 

    } 

그러나 슬프게도 : 다음

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

그리고 나는이 같은 작업 방법의 실제 잠금을 처리 할 수 ​​있다고 생각했다 :

나는이 방법이 어떻게 시작하려면 코드가 회전하여보기를 지시하는 전자에 흔들리지 않습니다 ...

이 모든 것이 잘못 될까요? 그것을하는 더 좋은 방법은 무엇입니까? 사용자가 화면의 방향을 잠글 수있게하기위한 로컬 쉬운 방법을 원합니다. 나는 잠금 버튼을 눌렀을 때 부울 값을 사용하여 잠금을 해제하고 다시 누를 것이라고 추측합니다 ...

생각 하시겠습니까? 감사합니다.

답변

3

shouldAutorotateToInterfaceOrientation 귀하의 로직을 귀하의 응용 프로그램 대리인 (또는 예를 반환 할 수있는 가장 수석 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 
} 
+1

번호가이 방법은 지원 방향에 대해 YES를 반환해야합니다 귀하의 경우 항상 YES 또는 항상 NO 중 하나를 반환합니다. – Eiko

+0

@Eiko, 현재 쓰여지고있는 방식으로 잠금이 활성화되어 있으면보기 컨트롤러가 모든 방향으로 회전하고 (잠금이 활성화되지 않은 경우 YES를 반환 함) 잠금이 전혀 회전하지 않도록 할 수 있습니다. 왜 이것이 downvote받을 가치가 있는지 모르겠다? – Jasarien

+0

에코 (Eiko)는 맞았습니다. 수정본을 편집했지만. –