2011-01-21 3 views
12

내 애플 리케이션에서 장치 회전 (방향 변경)의 경우 CCScene myscene에서 몇 가지 메서드를 호출하고 싶습니다. 내가 autorotation을 비활성화했습니다.장치 방향 (인터페이스 방향이 아님) 이벤트에서 자체 등록 방법?

문제는 : 장치 방향에 따라 장면의 중력을 변경하고 싶습니다. 내 코드 :.

-(void) onEnter 
{ 
    [super onEnter]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
} 
-(void) onExit 
{ 
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil]; 
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
} 
-(void)notification_OrientationWillChange:(NSNotification*)n 
{ 
    orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue]; 
} 
-(void)notification_OrientationDidChange:(NSNotification*)n 
{ 
    if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     b2Vec2 gravity(0, -10); 
     world->SetGravity(gravity); 
    } 
    if (orientation == UIInterfaceOrientationLandscapeRight) { 
     b2Vec2 gravity(0, 10); 
     world->SetGravity(gravity); 
    } 
} 

그러나이 경우 내가에만 자동 회전의 경우 활성화에 알림을받을 수 있습니다

(경우 장애인, 장치가 실제로이 상태 표시 줄의 방향을 변경하지 마십시오) 당신이 날 도와 드릴까요?

답변

28
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(orientationChanged:) 
              name:@"UIDeviceOrientationDidChangeNotification" 
              object:nil]; 



- (void)orientationChanged:(NSNotification *)notification{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    //do stuff 
    NSLog(@"Orientation changed");   
} 

상태 표시 줄이 아닌 장치 방향을 확인해야합니다.

typedef enum { 
     UIDeviceOrientationUnknown, 
     UIDeviceOrientationPortrait,   // Device oriented vertically, home button on the bottom 
     UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 
     UIDeviceOrientationLandscapeLeft,  // Device oriented horizontally, home button on the right 
     UIDeviceOrientationLandscapeRight,  // Device oriented horizontally, home button on the left 
     UIDeviceOrientationFaceUp,    // Device oriented flat, face up 
     UIDeviceOrientationFaceDown    // Device oriented flat, face down 
    } UIDeviceOrientation; 
+1

누락 된'['. – Macarse

+0

장치 방향 사용이 너무 민감하므로 가로 45 도가되면 세로 방향으로 변경됩니다. – danfordham

+0

그래서 ** 인터페이스 방향 **이 아닌 ** 장치 방향 **이라고합니다. 모바일 장치는 보유하고있는 방식에 따라 매우 자주 그 방향을 변경할 수 있습니다. 인터페이스 지향에 관심이있는 사람들은 UIViewController의 속성을 사용해야합니다. :) – nacho4d

관련 문제