這是我在iPhone中使用CALayer,Core Animation框架編寫的Objective-C中3D立方體的第二個問題。對於我的第一個問題,請訪問3D Cube Problem! Part 1。3D立方體問題,第2部分
我使用布拉德拉鬆斯代碼從這個鏈接
http://www.sunsetlakesoftware.com/2008/10/22/3-d-rotation-without-trackball
問題是我的立方體旋轉在x軸沿圖中所示的粉色系旋轉,我的3D立方體。
但我想繞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。如果你想知道我是如何創建這個立方體,那麼讓我知道
我很想知道您是如何創建立方體的。我正在嘗試爲你做類似的事情。目前爲止有何進展? – Jeremy 2011-07-21 23:59:21
我已經搞清楚了這個項目,但是我想我可以給你代碼來製作石英芯的立方體。 – Robin 2011-07-22 04:39:34
嘿,我得到了一個非常相似的代碼,旋轉'CALayer'三維和所有,但我解決了這個問題,通過改變'anchorPoint'西蒙李建議。你一定是做錯了。請分享圖層構建代碼,我會解決它......或者,如果需要,我可以自己做立方體。 – Mazyod 2012-11-25 17:07:11