2013-01-22 76 views
0

我在屏幕上有一個輪子和可旋轉的輪子。當用戶在輪子上滑動時,它應該根據滑動方向開始旋轉。我完成了除旋轉方向(順時針或逆時針)之外的所有功能。任何人都可以幫助我找到用戶在方向盤上滑動的方向。檢測旋轉輪的方向iPhone

由於

回答

0
_direction = 1; 
if(_startPoint.y < rotarCenter.y) { 

    if(_startPoint.x > endTouchPoint.x) 
     _direction = -1; 
} 
else if(_startPoint.y > rotarCenter.y) { 

    if(_startPoint.x < endTouchPoint.x) 
     _direction = -1; 
} 

if(_startPoint.x < rotarCenter.x && endTouchPoint.x < rotarCenter.x) { 

    if(_startPoint.y < endTouchPoint.y) 
     _direction = -1; 
    else 
     _direction = 1; 
} 
else if(_startPoint.x > rotarCenter.x && endTouchPoint.x > rotarCenter.x) { 

    if(_startPoint.y > endTouchPoint.y) 
     _direction = -1; 
    else 
     _direction = 1; 
} 
0

旋轉

解決方案1:

順時針

  • 如果觸摸point.y是上述車輪中心和滑動距離小的觸摸point.x到更大的x。或
  • 如果point.y位於輪心以下,並且滑動從較大點x到較小x。

逆時針:
的oppsite:

  • 如果point.y低於車輪中心和滑動距離小的觸摸point.x更大X。或
  • 如果point.y位於輪心以上,並且滑動是從較大點x到較小x。

有更好的解決方案,用更復雜的數學,但其中一個是簡單的。

更新這一個也很簡單,也更好!

解決方案2:

Caculate輪中心(X,Y)觸摸(X,Y)之間的角度:

double angleRad = atan2(dy/dx), where dy = touch.y - wheel.y, and dx analog. 

做,對於每個觸摸事件:如果角度然後順時針或逆時針

+1

請記住,如果您嘗試使輪子動起來,您必須以增量進行動畫,因爲任何超過180度的旋轉都需要t他最短的路徑(即向後)。 –

+0

@AlexWien非常感謝您的幫助AlexWien。它真的幫助我。我還爲y座標中的更改添加了更多條件,如下面的代碼所示 –