【问题标题】:Determine the Device Orientation and its resulting Angle on one Dimension?在一维上确定设备方向及其产生的角度?
【发布时间】:2015-10-17 01:25:15
【问题描述】:

我有以下设置:

iPhone 放置在桌面上,显示屏朝向天花板(alpha = 0 度)。当 iPhone 向上移动时,如上图所示,alpha 角会增加。

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

如何获得 iPhone 从桌子上抬起时的正确 alpha 角度?当 alpha 的值发生变化时如何收到通知?

【问题讨论】:

    标签: ios objective-c iphone swift gyroscope


    【解决方案1】:

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

    目标 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");
    }
    

    斯威夫特

    // 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 (一如既往)是一个很好的信息来源,CMDeviceMotion 上的文章也不例外。

    【讨论】:

    • 谢谢您可能对这个相关问题也有想法吗? stackoverflow.com/questions/31643371/…
    • 如何将更新发送到后台队列?您能否扩展您的示例
    • @confile 在linked NSHipster article 中描述了后台队列的使用。我也更新了答案。
    • @SteveWilford 它为 alpha 或 180 - alpha 提供正值。有什么方法可以区分它们吗?
    【解决方案2】:

    斯威夫特 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")
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多