Tuesday, 15 March 2011

ios - Measuring tilt angle with CMMotionManager -


Suppose that the iPhone / iPad standing in front of you in front of the screen, in Portrait Orientation. You tilt the device aside, put the screen in front of you. How do you measure the stationary inclination angle using CMMotionManager? This is a simple question that should be a simple answer, yet I can not find any method that does not disappear in quadrangle and rotation matrix.

Can someone give me an indication of working for an example?

see gravity :

  self .deviceQueue = [[NSOperationQueue alloc] init]; Self.motionManager = [[CMMotionManager alloc] init]; Self.motionManager.deviceMotionUpdateInterval = 5.0 / 60.0; // uideware * device = [uidwise current device]; [Self. Motion Manager StartDevice MotionApp Upping Refrymem: CMTTTRAFRAMXrbitroxVertical To Quote: SophieDeviceWithHandler: ^ (CMDVicomotion * Motion, NSERR * Error) {[NSOperation QE Main Q} Adoption With Block: ^ {CGFlot X = Gwolta Gravity.x; CGFloat y = motion.gravity.y; CGFloat z = motion.gravity.z; }]; }];   

With this reference frame ( CMAttitudeReferenceFrameXArbitraryZVertical ), if z is near zero, then you land it with a plane straightened up (As if you were holding it against the wall) and as you rotate on that plane, change the x and y values ​​are vertical where x is near zero and y is close to -1.


Looking at this, I think that if you want to change this vector in angles, you can use the following algorithm.

If you want to calculate how many degrees from the vertical, then the device has been rotated (where the positive is the clockwise, the negative answer is), you can calculate it as:

  // This zoom rotates around the axis CGFloat angle = atan2 (y, x) + M_PI_2; CGFLOT ANGELGREGGE = angle * 180.0F / MPI in // radian; How often do the footage through   
         You can use it to find out alternately:  self.view.layer.transform = catersform 3 rotate (serration transform 3-dendenti, -roth radian, 0, 0, 1);   

(Personally, I update rotation angle in the startDeviceMotionUpdates method, and conversion in CADisplayLink , which decodes the screen updates with the angle update.)

You can see how you tilted it backward / forward:

  // This tilted forward and backward CGFloat R = sqrtf (x * x + y * y + z * z); CGFloat tiltForwardBackward = acosf (z / r) * 180.0f / M_PI - 90.0f;    

No comments:

Post a Comment