我正在开发一个应用程序,当用户打开时会有一张图片,由CMMotionManager控制,根据用户倾斜设备的方向移动。
这是我用来控制图像与设备运动相对应的代码。
当用户打开应用程序时,图像通常位于起始位置的右侧或左侧90度,或者始终完全居中。我认为问题在于“CMAttitudeReferenceFrameXArbitraryCorrectedZVertical”,但我也尝试了“CMAttitudeReferenceFrameXArbitraryZVertical”,它也不起作用。如果有用的话,我的项目在这里,对任何感兴趣的人都可以参考。我还使用了苹果公司pARk的示例代码。
..
..
这是启动设备移动的代码。
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”,它也不起作用。如果有用的话,我的项目在这里,对任何感兴趣的人都可以参考。我还使用了苹果公司pARk的示例代码。