【发布时间】:2011-06-08 12:25:22
【问题描述】:
这是我在 iphone 中使用 CALayer、Core Animation 框架、用 Objective-c 编写的 3D 立方体的第二个问题。对于我的第一个问题,请访问这里3D Cube Problem! Part 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;
}
previousLocation 是在 touchesBegan: 方法中初始化的 CGPoint,currentLayer 是 CALayer,我在其中创建了这个多维数据集。
感谢您的帮助。
PS。如果你想知道我是如何创建这个立方体的,请告诉我
【问题讨论】:
-
我很想知道您是如何创建立方体的。我正在尝试对你做类似的事情。到目前为止有什么进展吗?
-
我已经完成了这个项目,但我想我可以给你在石英芯中制作立方体的代码。
-
嘿,我得到了一个非常相似的代码,在 3D 中旋转
CALayer等等,但我按照 Simon Lee 的建议通过更改anchorPoint解决了这个问题。你一定是做错了什么。请分享层构建代码,我会解决它...或者,如果需要,我可以自己做立方体。 -
那不是真的,我试过了,结果还是一样。可能还有其他我错过的东西。
-
那么,你是要分享代码,还是我应该构建一个 3D 立方体并旋转它?
标签: iphone objective-c core-animation calayer cube