2011-12-13 56 views
0

我已經開發爲iPhone 3D旋轉木馬,看起來很像下面的圖片: enter image description here如何計算從3D旋轉木馬動畫的視圖的位置?

我基本上創造UIViews的數組,然後註冊爲觸摸事件:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // if we're not already tracking a touch then... 
    if(!trackingTouch) 
    { 
     // ... track any of the new touches, we don't care which ... 
     trackingTouch = [touches anyObject]; 
    } 
} 

雖然運動仍在註冊我做到以下幾點:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // if our touch moved then... 
    if([touches containsObject:trackingTouch]) 
    { 
     // use the movement of the touch to decide 
     // how much to rotate the carousel 
     CGPoint locationNow = [trackingTouch locationInView:self]; 
     CGPoint locationThen = [trackingTouch previousLocationInView:self]; 

     lastAngle = currentAngle; 
     currentAngle += (locationNow.x - locationThen.x) * 180.0f/self.bounds.size.width; 

     // and update the view positions 
     [self setCarouselAngle:currentAngle]; 
    } 
} 

當觸摸結束我隨後致電:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // if our touch ended then... 
    if([touches containsObject:trackingTouch]) 
    { 
     // make sure we're no longer tracking it 
     trackingTouch = nil; 

     // and kick off the inertial animation 
     animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(animateAngle) userInfo:nil repeats:YES]; 
    } 
} 

的animageAngle方法基本上然後調用其他方法與由視圖陣列中的數除以有效360度,並使用餘弦和正弦函數,以產生x和y座標。

上述所有工作好但我有困難理解完成以下所需的數學:

  1. 單擊視圖出來的主要焦點在前面例如,在紫色的觀點,即不圖像
  2. 旋轉紫觀點成爲關注的焦點即前
  3. 加載一次旋轉一個新的視圖控制器具有完整

我明白,我必須CA計算當前位置的視圖與計劃的目的地之間的角度以進行旋轉,但我無法爲我的生活弄清楚。

先前的困惑道歉 - 我一直在尋找這個問題,因爲我已經開始失去這個陰謀。

謝謝

+0

你將需要提供某種形式的圖像來描述這一點,因爲我不能形成你的情況在我的頭上了良好的畫面。另外,CGAffineTransform如何實現這一點,因爲它只是一個二維變換?看起來你需要在這裏使用CATransform3D。 –

+0

感謝Brad回覆。我在第一篇文章中誤導了我,因爲這不僅僅是3D效果的印象。希望圖像可以幫助更多 – JordanMazurke

回答

0

終於....我設法破解它。

我的表現令人難以置信的愚蠢,並沒有邏輯思維。我基本上重新跑的NSTimer調用運行在currentAngle中屬性下面的計算(在僞代碼)的方法:

CGFloat currentLocation; 
CGFloat nextLocation; 
CGFloat destinationLocation; 

currentLocation = viewImInterestedIn.frame.origin.x; 
nextLocation = currentLocation - 1; //or + 1 if the selected view is to the left of centre 

destinationLocation = 115.0f //set this to a default value 
currentAngle += (currentLocation - nextLocation) * 180.0f/self.bounds.size.width; 

// and update the view positions 
[self setCarouselAngle:currentAngle]; 

的Et瞧!

非常簡單 - 我必須學會退後一步,回頭再看問題。

感謝所有