3D立方体问题,第2部分

Rob*_*bin 6 iphone core-animation objective-c cube calayer

这是我在iphone中使用CALayer,Core Animation框架编写3D立方体的第二个问题,用Objective-c编写.对于我的第一个问题,请访问这里3D立方体问题!第1部分.

我正在使用Brad Larsons代码从此链接旋转我的3D立方体

http://www.sunsetlakesoftware.com/2008/10/22/3-d-rotation-without-trackball

问题是我的立方体沿x轴旋转,沿着图中所示的粉红线.

在此输入图像描述

但我想沿着图中所示的黑线围绕x轴旋转它.
现在在我的代码中我没有在我的视图上绘制任何粉红色线条或黑色线条,所以有人可以帮助我.

如果它有帮助,这里是用于在touchesMoved:方法中旋转我的立方体的代码

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint location = [[touches anyObject] locationInView:self];
    CATransform3D currentTransform = currentLayer.sublayerTransform;
    CGFloat displacementInX = location.x - previousLocation.x;
    CGFloat displacementInY = previousLocation.y - location.y;
    CGFloat totalRotation = sqrt(displacementInX * displacementInX + displacementInY * displacementInY);
    CGFloat x = (displacementInX/totalRotation) * currentTransform.m12 + (displacementInY/totalRotation) * currentTransform.m11;
    CATransform3D rotationalTransform = CATransform3DRotate(currentTransform, totalRotation * M_PI / 180.0, x, y, 0);
    currentLayer.sublayerTransform = rotationalTransform;
}
Run Code Online (Sandbox Code Playgroud)

previousLocation是一个CGPoint初始化的touchesBegan:方法,currentLayer是CALayer我创建这个多维数据集的地方.

谢谢你的帮助.

PS.如果您想知道我是如何创建这个多维数据集的,请告诉我

小智 4

看起来您需要在旋转图层之前先平移图层。

您可以先使用 CATransform3DTranslate 将图层从其自然原点平移,然后再使用 CATransform3DRotate,而不是选择枢轴/锚点。

了解如何构建多维数据集以查明如何实现它会有所帮助。