使用Core Motion和CATransform3D旋转图像

3

我正在尝试设计一个应用程序,使其看起来好像可以查看物体的各个方面。我已经做了很多工作,但是我无法弄清楚如何使物体浮在空中,就像一张照片框架一样(而不是平放在地上)。

这是我的代码:

CMMotionManager *motionManager = [[CMMotionManager alloc] init];
[motionManager setDeviceMotionUpdateInterval:0.1];

CMDeviceMotionHandler  motionHandler = ^ (CMDeviceMotion *motion, NSError *error) 
{
    [xLabel setText:[NSString stringWithFormat:@"X: %f, Yaw: %f", motion.userAcceleration.x, motion.attitude.yaw]];
    [yLabel setText:[NSString stringWithFormat:@"Y: %f, Pitch: %f", motion.userAcceleration.y, motion.attitude.pitch]];
    [zLabel setText:[NSString stringWithFormat:@"Z: %f, Roll: %f", motion.userAcceleration.z, motion.attitude.roll]];

    CATransform3D transform;
    transform = CATransform3DMakeRotation(motion.attitude.pitch, 1, 0, 0);
    transform = CATransform3DRotate(transform, motion.attitude.roll, 0, 1, 0);
    transform = CATransform3DRotate(transform, motion.attitude.yaw, 0, 0, 1);

    myView.layer.sublayerTransform = transform;
    //[myImage setNeedsDisplay];
};

[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];

这将使图片看起来像是平放在桌子上,但实际上应该看起来像是挂在墙上。有什么好的想法吗?

1个回答

1
如果看起来是平的,你只需要在俯仰旋转代码中添加正确的旋转来使其默认竖立。尝试将你的俯仰旋转更改为下面的代码: transform = CATransform3DMakeRotation(motion.attitude.pitch + M_PI_2, 1, 0, 0); 如果它倒置了,尝试进行减法运算。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接