2017-05-10 30 views
0

我用下面的代碼,但一直給我0值,請檢查什麼我做錯了目標C - 裝置運動姿態值

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]; 

感謝您的幫助

+0

任何幫助會感激 – user460024

+0

你如何初始化'self.motionManager'問題的選項? – Losiowaty

+0

@property(非原子,強)CMMotionManager * motionManager; – user460024

回答

1

首先我會嘗試,如果所有對象不是零,然後檢查你實際運行在MotionManager本身,是這樣的:

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

下一個問題可能是,如果你的代碼在主線程

上運行

有很多這可以使你有