2012-04-18 50 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

相关问题