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

时间:2011-06-08 12:25:23

标签: iphone objective-c core-animation calayer cube

这是我在iphone中使用CALayer(Core动画框架)编写3D立方体的第二个问题,用Objective-c编写。关于我的第一个问题,请访问3D Cube Problem! Part 1

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

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

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

enter image description here

但是我想沿着图中所示的黑线围绕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;
}

previousLocation是在CGPoint方法中初始化的touchesBegan:,而currentLayer是CALayer,我创建了此多维数据集。

感谢您的帮助。

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

2 个答案:

答案 0 :(得分:3)

将andchorPoint设置为中心;如果处理CALayer然后......

[myLayer setAnchorPoint:CGPointMake(0.5, 0.5)];

答案 1 :(得分:3)

看起来你需要在旋转它们之前先转换它们。

您可以先使用CATransform3DTranslate将图层转换为远离其自然原点,而不是选择一个轴/锚点,然后使用CATransform3DRotate。

有助于了解如何构建多维数据集以确定如何实现它。