2013-01-05 31 views
0

我有一個應用程序,當用戶更改設備的位置時,我正在使iOS設備發出聲音。我讓用戶將設備放在平坦的位置,橫向位置和縱向位置,當用戶在適當的時間將設備放置在這些位置時,應用會發出聲音以確認位置。使用UIDeviceOrientationDidChangeNotification時,iOS設備在正確位置時不會反應

但是,我的問題是,如果設備最初處於平坦的位置(例如平躺在桌子上),則設備不會發出聲音。換句話說,只有當用戶在該位置打開設備並且不立即感測到它已經位於的位置時才發出聲音。我是否有辦法糾正這種情況,以便應用程序在最初處於該位置或放入正確位置時發出聲音?這裏是我的相關代碼:

- (void) orientationCheck { 

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

    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(showTextOnly) userInfo:nil repeats:NO]; 
} 


- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice *device = note.object; 

    if(currentTest == flatTest) { 

     testLabel.frame = CGRectMake(50, 30, 200, 150); 
     [testLabel setText:@"Hold device on its side."]; 
     testImage.frame = CGRectMake(10, 150, 300, 200); 
     testImage.image = [UIImage imageNamed:@"accelerometer test - side.png"]; 

     if (device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown) { 

      ... 

     } 

    } 

任何人都可以看到我做錯了什麼,我該如何解決這個問題?

回答

0

爲什麼不在啓動時檢查設備的方向?換句話說,除了在設備方向改變時註冊通知之外,請看設備方向

+0

錯誤...這是有道理的。我什麼時候該做檢查,在行之前:[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];或之後,但在行之前:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged :) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]]; ? – syedfa

+0

無關緊要。這些是關於將來會發生什麼的命令。你想知道現在什麼是真的。 – matt

相關問題