【问题标题】:Objective c - device motion attitude valuesObjective c - 设备运动姿态值
【发布时间】:2017-05-10 11:56:53
【问题描述】:

我正在使用以下代码,但一直给我 0 值请检查我做错了什么

UILabel *rollLabel = [[UILabel alloc] init];
CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
float myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
float myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
float myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
[rollLabel setText:[NSString stringWithFormat:@"ROLL = %f , - Pitch = %f, , - Yaw = %f", myRoll, myPitch, myYaw]];
rollLabel.frame = CGRectMake(0, 0, 400, 150);
rollLabel.backgroundColor=[UIColor greenColor];
rollLabel.textColor=[UIColor blackColor];
[self.view addSubview:rollLabel];

感谢您的帮助

【问题讨论】:

  • 任何帮助将不胜感激
  • 如何初始化self.motionManager
  • @property (nonatomic, strong) CMMotionManager *motionManager;
  • 这只是一个声明,只有这个你的motionManager 将是nil
  • @Losiowaty 如何做到这一点,我正在从网上获取代码...请帮助回答谢谢

标签: ios objective-c devicemotion


【解决方案1】:

首先我会尝试如果所有对象都不为零,然后检查你是否真的运行了motionManager本身,比如:

_motionManager = [[CMMotionManager alloc] init];
_motionManager.deviceMotionUpdateInterval = 1.0/60;
[_motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];

下一个问题可能是你的代码是否在主线程上运行

有很多选项可能会导致您遇到的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2010-11-03
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多