2013-11-27 57 views
3

我正在开发一个应用程序,当用户打开时,将会有一个由CMMotionManager控制的图像,该图像根据用户倾斜设备的方向进行移动。CMMotionManager参考框架

start image ... user tilts down ... user tilts right

这是我的代码启动装置运动。

motionManager = [[CMMotionManager alloc] init]; 
motionManager.showsDeviceMovementDisplay = YES; 

motionManager.deviceMotionUpdateInterval = 1.0/60.0; 

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical]; 

这是我用来控制我的图像参考设备的运动的代码。

- (void)drawRect:(CGRect)rect 
{ 
    if (placesOfInterestCoordinates == nil) { 
     return; 
    } 


    mat4f_t projectionCameraTransform; 
    multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform); 

    int i = 0; 


    for (MovingImage *poi in [placesOfInterest objectEnumerator]) { 
     vec4f_t v; 

     multiplyMatrixAndVector(v, projectionCameraTransform, placesOfInterestCoordinates[i]); 

     float x = (v[0]/v[3] + 1.0f) * 0.5f; 
     float y = (v[1]/v[3] + 1.0f) * 0.5f; 

     if (v[2] < 0.0f) { 
      CGPoint movingTo = CGPointMake(x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height); 

      if (movingTo.x < -118) { 
       movingTo.x = -118; 
      } 
      if (movingTo.x > 542) { 
       movingTo.x = 542; 
      } 
      if (movingTo.y < 215) { 
       movingTo.y = 215; 
      } 
      if (movingTo.y > 390) { 
       movingTo.y = 390; 
      } 

      poi.view.center = movingTo; 

      poi.view.hidden = NO; 

     } else { 
      poi.view.hidden = YES; 
     } 
     i++; 
    } 
} 

的图像是在屏幕的中间很少当用户打开应用程序时,图像通常是向右或向左旋转90度的起始位置,或者正好在中间一致。

我认为CMAttitudeReferenceFrameXArbitraryCorrectedZVertical是问题,但我也试过CMAttitudeReferenceFrameXArbitraryZVertical这也不起作用。 如果有用,我的项目是here,对于任何有兴趣的人。我也使用Apple pARk示例代码。

回答

0
- (void)drawRect:(CGRect)rect 
{ 
if (placesOfInterestCoordinates == nil) { 
    return; 
} 


mat4f_t projectionCameraTransform; 
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransform); 

int i = 0; 


for (MovingImage *poi in [placesOfInterest objectEnumerator]) { 
    vec4f_t v; 

    multiplyMatrixAndVector(v, projectionCameraTransform, placesOfInterestCoordinates[i]); 

    float x = (v[0]/v[3] + 1.0f) * 0.5f; 
    float y = (v[1]/v[3] + 1.0f) * 0.5f; 

    if (v[2] < 1.0f) { // change here 
     CGPoint movingTo = CGPointMake(x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height); 

     if (movingTo.x < -118) { 
      movingTo.x = -118; 
     } 
     if (movingTo.x > 542) { 
      movingTo.x = 542; 
     } 
     if (movingTo.y < 215) { 
      movingTo.y = 215; 
     } 
     if (movingTo.y > 390) { 
      movingTo.y = 390; 
     } 

     poi.view.center = movingTo; 

     poi.view.hidden = NO; 

    } else { 
     poi.view.hidden = YES; 
    } 
    i++; 
} 
} 

尝试改变(v[2] < 0.0f)(v[2] < 1.0f)

的视角是不同的,因为它正在寻找时,图像映入眼帘,比你的设备的角度来看,角度等。