2011-06-08 53 views
6

這是我在iPhone中使用CALayer,Core Animation框架編寫的Objective-C中3D立方體的第二個問題。對於我的第一個問題,請訪問3D Cube Problem! Part 13D立方體問題,第2部分

我使用布拉德拉鬆斯代碼從這個鏈接

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

問題是我的立方體旋轉在x軸沿圖中所示的粉色系旋轉,我的3D立方體。

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是touchesBegan:方法初始化的CGPoint和currentLayer是CALayer,我創造了這個立方體。

感謝您的幫助。

PS。如果你想知道我是如何創建這個立方體,那麼讓我知道

+0

我很想知道您是如何創建立方體的。我正在嘗試爲你做類似的事情。目前爲止有何進展? – Jeremy 2011-07-21 23:59:21

+1

我已經搞清楚了這個項目,但是我想我可以給你代碼來製作石英芯的立方體。 – Robin 2011-07-22 04:39:34

+0

嘿,我得到了一個非常相似的代碼,旋轉'CALayer'三維和所有,但我解決了這個問題,通過改變'anchorPoint'西蒙李建議。你一定是做錯了。請分享圖層構建代碼,我會解決它......或者,如果需要,我可以自己做立方體。 – Mazyod 2012-11-25 17:07:11

回答

3

看起來你需要先旋轉層先旋轉它們。

您可以首先使用CATransform3DTranslate將圖層轉換爲遠離其自然原點,並在此之後使用CATransform3DRotate,而不是選取主點/錨點。

這將有助於看看如何構建立方體以查明它如何實現。

+0

讓我嘗試一下,然後在這個問題上回復你。 – Robin 2012-11-26 03:40:55

+0

嘿,實際工作,感謝您的幫助。不知道那會很簡單。謝謝。 – Robin 2012-11-29 05:46:06

3

將andchorPoint設置爲中心;如果處理CALayer然後...

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

在圖中還沒有沿着那條粉紅線旋轉 – Robin 2011-06-08 12:31:32