2012-04-18 43 views
0

我已經成功地實現這個職位旋轉使用此代碼一個UIImageView的UIImageView如何跟蹤順時針或逆時針用戶運動

iPhone - Wheel tracking finger jumps to same starting position?

我同胞的開發人員的問題是,在觸摸的用戶既可以旋轉圖像順時針和逆時針。有沒有一種方法可以檢測用戶移動視圖的方向?

它對我很重要,因爲我正在製作一個時鐘應用程序。我讓用戶將分針形式從0分鐘一直移動到60分鐘。當它到達那裏時,我將時針向上移動一次。但用戶可以逆時針旋轉分針,在這種情況下,我需要將時針向下移動一下。有任何想法嗎?

+0

我沒仔細看過那個帖子最後一次,但我注意到接受的答案計算了fromAngle和toAngle。這些差異的跡象似乎是你需要的。 – danh 2012-04-18 00:48:55

+0

這不起作用。在一個圓圈內,你不能依靠角度來看物體旋轉的方向,這意味着如果我將物體從270度旋轉到270度,我怎麼知道我旋轉物體的方式? – 2012-04-18 01:24:58

回答

3

你可以設置一個名爲「lastPoint」的成員變量來記錄點你感動

的,你可以calc下下一次的方向


CGPoint lastPoint; 


- (void) touchedBegin:(NSSet *)touches withEvent:(UIEvent *)event { 
    lastPoint = [touch locationInView:self.view]; 
} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
NSSet *allTouches = [event allTouches]; 

//you can save the point , then used in next time you want 

int tInput = [allTouches count]-1; 

UITouch *touch =[[allTouches allObjects] objectAtIndex:tInput]; 
CGPoint location = [touch locationInView:self.view]; 
float theAngle = atan2(location.y-imageX.center.y, location.x-imageX.center.x); 

float theLastAngle = atan2(lastPoint.y-imageX.center.y, lastPoint.x-imageX.center.x); 

lastPoint = location; 

// compare theAngle & theLastAngle , so you can know it is clockwise or counterclockwise. 
} 

+0

謝謝,但我自己想到了這一點。 – 2012-04-18 16:23:24

+2

@SamBudda:如果你認爲這是你自己的,如果你發佈你的答案,這對傳播會是公平的。如果您選擇不接受,至少提供一個很好的答案也是一個很好的做法。從我+1到adali。 – 2012-06-30 09:01:59

相關問題