2009-04-08 4 views
7

다음을 구현하고 싶습니다. 사용자가 iPhone을 회전시킬 때 새로운 UIViewController (회전시 자동으로 버튼을 클릭하거나 유사한 작업 수행하지 않음)를 인스턴스화하고이 새로운 UIViewController가 가로 방향으로 처리 한 뷰를 사용자에게 보여주고 싶습니다. 이 작업을 올바르게 수행하는 방법은 무엇입니까?iPhone을 회전시키고 새로운 UIViewController를 인스턴스화 하시겠습니까?

willRotateToInterfaceOrientation 및 didRotateFromInterfaceOrientation 메서드에서 새 컨트롤러를 인스턴스화하려고 시도했지만이 메서드 중 아무 것도 호출되지 않습니다. 나는 현재 컨트롤러가 tabBarController에 의해 처리되는 네비게이션 컨트롤러에 의해 푸시 되었기 때문에 이것이 의심 스럽다. 어떤 단서? 간단한 코드 스 니펫이 크게 감사 할 것입니다.

미리 감사드립니다.

답변

7

응용 프로그램에서이 정확한 유형의 동작을 구현했습니다. 핵심은 상위 컨트롤러가 shouldAutorotateToInterfaceOrientation을 구현하고 현재보기 컨트롤러에서도이를 구현하는지 확인하는 것입니다. 제 경우에는 shouldAutorotateToInterfaceOrientation에 대한 호출을 가로 채는 탭 컨트롤러를 사용하고 있습니다. 탭 컨트롤러를 재정 의하여 호출이 중단되도록해야했습니다. 보기 컨트롤러의 기본 동작은 세로 모드로만 표시됩니다.

그래서 모든 방향을 통해 변경 허용하도록 강제해야합니다 - (BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) interfaceOrientation { 반환 YES; }

그런 다음에 응답해야 새로운 뷰 컨트롤러로드하려면 :

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || 
    (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) 
    {  
    // Load the view controller you want to display in landscape mode... 
    } 
} 

수는 사용을 :

-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

는 방향 변화가오고 있음을 감지합니다.

내 경우에는 didRotateFromInterfaceOrientation을 사용하여 마지막 방향이 세로인지 확인하고 가로 방향으로 표시하려는보기를로드합니다.

그런 다음 viewcontroller에서 동일한 메소드를 구현했습니다.로드하는 중입니다. 방향이 세로 방향으로 바뀌고 뷰 스택에서 벗어날 때를 감지합니다.

조금 도움이 되길 바랍니다.

+0

shouldAutorotateToInterfaceOrientation 메소드를 추가하기 위해 초기 tabBarController를 서브 클래스 화해야합니까? 이 탭바 컨트롤러는 IB와 응용 프로그램 대리인의 코드 몇 줄을 사용하여 설정되었습니다. 모든 단계를 수행했지만 didRotateFromInterfaceOrientation이 호출되지 않았습니다. –

+1

자, tabBarController를 서브 클래 싱하고 shouldAutorotateToInterfaceOrientation을 추가하면 모든 것이 작동합니다. 당신의 도움을 주셔서 대단히 감사합니다. 친절합니다. –

+0

나는 UITabBarController를 전혀 사용하지 않고 UIViewController를 UITabBar와 함께 사용하는 것을 선호합니다. – mk12

1

shouldAutorotateToInterfaceOrientation을 구현합니까? 그렇지 않다면 메시지를받지 못하는 것일 수 있습니다.

+0

예 내가 할. shouldAutorotateToInterfaceOrientation에서 단순히 YES를 반환해도 작동하지 않습니다. –

29

클리너 방법은 회전 UIInterfaceOrientation을 사용하지 않는 것입니다.

이유는 인터페이스 또는보기 1이 실제로 회전하기 때문입니다. 즉,보기 2를 제거한 후에 다시 표시하려면 다시 회전해야합니다.

이를 보완하기 위해 간단하게 UIDeviceOrientation 알림을 구독합니다. 미묘하게 다른. View 1은 자동 회전을 지원하지 않아도이 방법을 사용할 수 있습니다.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

가 그럼 그냥 메소드 detectOrientation을 정의 :

-(void) detectOrientation { 

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
      ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 

     //load view 2 


    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) { 

     // load view 1 

    } 
} 

이제 귀하의 의견 중 어느 것도 autoratation을 지원할 필요가있는 viewDidLoad에서 다음 코드를 입력합니다! 당신은 그러나로드하기 전에 뷰 2 변환을 수행해야합니다

-(void) transformView2ToLandscape { 

    NSInteger rotationDirection; 
    UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation]; 

    if(currentOrientation == UIDeviceOrientationLandscapeLeft){ 
    rotationDirection = 1; 
    }else { 
    rotationDirection = -1; 
    } 

    CGRect myFrame = CGRectMake(0, 0, 480, 300); 
    CGAffineTransform transform = [[self view2] transform]; 
    transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90)); 
    [[self view2] setFrame: myFrame]; 
    CGPoint center = CGPointMake(myFrame.size.height/2.0, myFrame.size.width/2.0); 
    [[self view2] setTransform: transform]; 
    [[self view2] setCenter: center]; 

} 

그게 어떻게 내보기에 자동 회전을 지원하지 않고 회전 오 의견을 교환합니다.

+1

이것은 정말 재미있어 보입니다. 실제 실적, 즉 알림이 전달되는 속도와 변환을 적용하는 데 얼마나 걸리는지를 이해하려고합니다. 대단히 감사합니다. –

+0

매우 잘 작동합니다. 특히 두 방향에 대해 두 개의 다른보기 컨트롤러를 사용하는 경우에 유용합니다. –

+0

"확인 된"대답이 질문에 대답하지만,이 대답은 (이상적으로) 질문해야하는 질문에 대한 대답입니다. – DBD

3

실제로 최고 수준 컨트롤러 만 통보됩니다. 중첩 된 컨트롤러에 알리는 것은 사용자의 책임입니다.

은 탭 표시 줄 컨트롤러에이 추가 :

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    [self.selectedViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
} 
+0

고맙습니다.이 코드를 코드에 추가하겠습니다. –

+0

UITabBarController를 하위 클래스 화하면 안됩니다. – mk12

+0

@ Mk12 : 이유가 무엇입니까? 그러한 작은 무시가 문제를 일으킬 수 있습니까? – Kornel

관련 문제