2011-11-09 54 views
3

我正在嘗試設計一個應用程序,使其顯示爲可以環視對象。我已經走得很遠了,但我無法弄清楚如何讓這個物體像一個畫框一樣漂浮在空中(而不是平坦的地面)。使用Core Motion和CATransform3D旋轉圖像

這裏是我的代碼:

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

如果看起來平坦,只需將正確的旋轉添加到音高旋轉代碼,以使其默認情況下保持直立。試着改變你的俯仰旋轉到下面的代碼

transform = CATransform3DMakeRotation(motion.attitude.pitch + M_PI_2, 1, 0, 0);

如果倒掛嘗試做減法來代替。