ios - CMMotionManager 引用框架

标签 ios objective-c opengl-es github core-motion

我正在开发一个应用程序,当用户打开时会有一个图像,由 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 ,对于任何感兴趣的人。我用苹果pARk示例代码。

最佳答案

- (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)

视角不同,因为它正在寻找图像进入视野的时间,而不是您设备的视角。

关于ios - CMMotionManager 引用框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251672/

相关文章:

ios - Facebook SDK 打开 SafariViewController 而不是 Facebook 应用程序

ios - Universal Link 智能横幅中的标题从何而来?

objective-c - 如何在不实际更改的情况下重新排列 char* 的开头和结尾(索引方式)?

iphone - UINavigationBar 两个圆角

android - OpenGL ES 2.0 渲染不正确

iphone - 如何在 GLSL (OpenGL ES 2.0) 中定义常量数组?

ios - 即使在应用程序终止后也能获取 Apple 的远程推送通知

ios - 如何使用从 iOS 中的服务器获取的 URL 在 UIView 中播放视频

iphone - TableView :didSelectRowAtIndexPath:]: message sent to deallocated instance 0xebba1b0

opengl-es - LibGDX - Sprite 到像素图