确定设备方向及其在一个维度上的角度?

Determine the Device Orientation and its resulting Angle on one Dimension?

我有以下设置:

一个 iPhone 位于 table 的天花板上(alpha = 0 度)。当 iPhone 向上移动时,如上图所示,alpha 角增加。

如何在不考虑任何其他可能改变的轴的情况下计算 alpha 角的值。我只对这个轴感兴趣。

如何获得 iPhone 从 table 抬起时的正确 alpha 角?如何在 alpha 值更改时收到通知?

您可以使用 CMMotionManager class 来监控设备运动变化。

Objective 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
self.motionManager = CMMotionManager()

if motionManager?.deviceMotionAvailable == true {
        
    motionManager?.deviceMotionUpdateInterval = 0.1
        
    let queue = OperationQueue()
    motionManager?.startDeviceMotionUpdatesToQueue(queue, withHandler: { [weak self] motion, error in
            
        // Get the attitude of the device
        if let attitude = motion?.attitude {
            // Get the pitch (in radians) and convert to degrees.
            print(attitude.pitch * 180.0/Double.pi)

            DispatchQueue.main.async {
                // Update some UI
            }
        }
            
    })
        
    print("Device motion started")
}else {
    print("Device motion unavailable")
}

NSHipster is (as always) a great source of information, and the article on CMDeviceMotion也不例外。

Swift 4:

  let motionManager = CMMotionManager()

   if motionManager.isDeviceMotionAvailable {

       motionManager.deviceMotionUpdateInterval = 0.1

       motionManager.startDeviceMotionUpdates(to: OperationQueue()) { [weak self] (motion, error) -> Void in

           if let attitude = motion?.attitude {

                print(attitude.pitch * 180 / Double.pi)

                DispatchQueue.main.async{
                    // Update UI
               }
           }

       }

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