2012-08-23 43 views
51

我有一個遊戲,其中設備的方向會影響遊戲的狀態。用戶必須快速在風景,人像和逆向風景方向之間切換。到目前爲止,我已經通過註冊的遊戲取向通知:立即檢測iOS方向變化

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

但它實在太慢 - 似乎有大約旋轉實際上被解僱的電話和通知之間的第二延遲。我需要一種方法來即時檢測設備方向的變化。我曾嘗試過使用陀螺儀進行實驗,但我還不太熟悉陀螺儀,以瞭解它是否是我正在尋找的解決方案。

+0

在這裏,你走了,使用這個[鏈接](http://stackoverflow.com/questions/5559652/how-do-i-detect-the-orientation-如果有任何疑問,請讓我知道。 – Sandy

+0

http://tech-blog.maddyzone.com/javascript/detect-orientation-event非常好的文章 –

回答

22

延遲你所談論的實際上是一個過濾器,以防止錯誤(不需要的)方向更改通知。

對於即時識別設備方向變化,你只能自己監視加速度計。

加速度計測量所有3個軸的加速度(包含重力),因此在計算實際方向時不應有任何問題。

一些代碼開始與加速度計的工作都可以在這裏找到:

How to make an iPhone App – Part 5: The Accelerometer

而這個不錯的博客涵蓋了數學部分:

Using the Accelerometer

+0

UIA加速度計已棄用,由Core Motion –

+1

替代。第二個鏈接已損壞。 –

21

爲什麼你didn`t使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

或者你可以用這個

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

或者這

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

希望貓頭鷹是有用的)

+8

這些方法不會在子視圖控制器上自動調用。 –

+1

現在這些方法已被棄用。 – Satyam

141

添加通知在viewWillAppear功能

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

取向變化通知該功能

- (void)orientationChanged:(NSNotification *)notification{ 
    [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; 
} 

,後者又調用此函數,其中moviePlayerController幀是取向在viewDidDisappear處理

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation { 

    switch (orientation) 
    { 
     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 
     { 
     //load the portrait view  
     } 

      break; 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
     { 
     //load the landscape view 
     } 
      break; 
     case UIInterfaceOrientationUnknown:break; 
    } 
} 

除去通知

-(void)viewDidDisappear:(BOOL)animated{ 
    [super viewDidDisappear:animated]; 
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

我想這是最快u能已經改變了看法按方向

+4

像真正的程序員一樣,不要忘記[超級viewWillAppear:動畫]和[超級viewDidDisappear:動畫]複製粘貼的情況下。 –

+0

注意:UIDeviceOrientation!= UIInterfaceOrientation。 – nnrales

5

儘量使你的變化:

- (void) viewWillLayoutSubviews {}

的代碼將運行隨着子視圖重新佈局,每一個方向都會發生變化。

5

@vimal答案沒有爲我提供解決方案。這個方向似乎不是目前的方向,而是以往的方向。爲了解決這個問題,我用[[UIDevice currentDevice] orientation]

- (void)orientationChanged:(NSNotification *)notification{ 
    [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]]; 
} 

然後

- (void) adjustViewsForOrientation:(UIDeviceOrientation) orientation { ... } 

有了這個代碼,我得到的當前方位位置。

10

對於我的案件處理UIDeviceOrientationDidChangeNotification是不好的解決方案,因爲它被稱爲更頻繁和UIDeviceOrientation並不總是等於UIInterfaceOrientation,因爲(FaceDown,FaceUp)。

我處理它使用UIApplicationDidChangeStatusBarOrientationNotification

//To add the notification 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:) 

//to remove the 
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 

... 

- (void)didChangeOrientation:(NSNotification *)notification 
{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

    if (UIInterfaceOrientationIsLandscape(orientation)) { 
     NSLog(@"Landscape"); 
    } 
    else { 
     NSLog(@"Portrait"); 
    } 
}