2015-07-27 6 views
7

I mają następujące parametry:Określa orientację urządzenia i wynikający z tego kąt w jednym wymiarze?

enter image description here

IPhone spoczywa na wyświetlaczu na suficie na stole (alfa = 0 °). Gdy iPhone zostanie przesunięty w górę, jak pokazano na obrazku powyżej, zwiększa się kąt alfa.

Jak obliczyć wartość kąta alfa bez dbania o inne osie, które mogą ulec zmianie. Interesuje mnie tylko ta jedna oś.

Jak uzyskać prawidłowy kąt alfa, jaki ma iPhone, gdy podnosi się ze stołu? Jak uzyskać powiadomienie o zmianie wartości alfa?

Odpowiedz

10

Możesz użyć klasy CMMotionManager, aby monitorować zmiany ruchu urządzenia.

Cel C

// Ensure to keep a strong reference to the motion manager otherwise you won't get updates 
self.motionManager = [[CMMotionManager alloc] init]; 
if (self.motionManager.deviceMotionAvailable) { 

    self.motionManager.deviceMotionUpdateInterval = 0.1; 

    // For use in the montionManager's handler to prevent strong reference cycle 
    __weak typeof(self) weakSelf = self; 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [self.motionManager startDeviceMotionUpdatesToQueue:queue 
              withHandler:^(CMDeviceMotion *motion, NSError *error) { 

               // Get the attitude of the device 
               CMAttitude *attitude = motion.attitude; 

               // Get the pitch (in radians) and convert to degrees.           
               NSLog(@"%f", attitude.pitch * 180.0/M_PI); 

               dispatch_async(dispatch_get_main_queue(), ^{ 
                // Update some UI 
               }); 

              }]; 

    NSLog(@"Device motion started"); 
} 
else { 
    NSLog(@"Device motion unavailable"); 
} 

Swift

// Ensure to keep a strong reference to the motion manager otherwise you won't get updates 
motionManager = CMMotionManager() 
if motionManager?.deviceMotionAvailable == true { 

    motionManager?.deviceMotionUpdateInterval = 0.1; 

    let queue = NSOperationQueue() 
    motionManager?.startDeviceMotionUpdatesToQueue(queue, withHandler: { [weak self] (motion, error) -> Void in 

     // Get the attitude of the device 
     if let attitude = motion?.attitude { 
      // Get the pitch (in radians) and convert to degrees. 
      // Import Darwin to get M_PI in Swift 
      print(attitude.pitch * 180.0/M_PI) 

      dispatch_async(dispatch_get_main_queue()) { 
       // Update some UI 
      } 
     } 

    }) 

    print("Device motion started") 
} 
else { 
    print("Device motion unavailable"); 
} 

NSHipster jest (jak zwykle) jest doskonałym źródłem informacji, oraz artykuł na CMDeviceMotion żaden wyjątek.

+0

Dziękujemy, masz pomysł na to również pytanie? http://stackoverflow.com/questions/31643371/how-can-i-draw-and-rotate-an-arrow-at-an-orientation-in-3d-space – confile

+0

Jak wysłać aktualizację do kolejki tła ? Czy mógłbyś rozszerzyć swój przykład? – confile

+0

@confile Używanie kolejki tła jest opisane w [połączonym artykule NSHipster] (http://nshipster.com/cmdevicemotion/#queueing-up). Zaktualizowałem również odpowiedź. –

Powiązane problemy