UITouch触摸移动手指方向和速度

如何在touchmoved函数中获得手指移动的速度和方向?

我想获取手指速度和手指方向,并将其应用于UIView类的方向移动和animation速度。

我读了这个链接,但是我不明白答案,另外也没有解释我如何检测方向:

UITouch移动速度检测

到目前为止,我试过这个代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *anyTouch = [touches anyObject]; CGPoint touchLocation = [anyTouch locationInView:self.view]; //NSLog(@"touch %f", touchLocation.x); player.center = touchLocation; [player setNeedsDisplay]; self.previousTimestamp = event.timestamp; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self.view]; CGPoint prevLocation = [touch previousLocationInView:self.view]; CGFloat distanceFromPrevious = [self distanceBetweenPoints:location :prevLocation]; NSTimeInterval timeSincePrevious = event.timestamp - previousTimestamp; NSLog(@"diff time %f", timeSincePrevious); } 

方向将由touchesMoved中的“location”和“prevLocation”的值确定。 具体而言,位置将包含触摸的新点。 例如:

 if (location.x - prevLocation.x > 0) { //finger touch went right } else { //finger touch went left } if (location.y - prevLocation.y > 0) { //finger touch went upwards } else { //finger touch went downwards } 

现在touchesMoved将被调用多次为一个给定的手指运动。 当手指第一次触摸屏幕时,将代码的关键是比较一个初始值,当运动最终完成时,用CGPoint的值进行比较。

为什么不把下面的作为obuseme的回应的变化

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *aTouch = [touches anyObject]; CGPoint newLocation = [aTouch locationInView:self.view]; CGPoint prevLocation = [aTouch previousLocationInView:self.view]; if (newLocation.x > prevLocation.x) { //finger touch went right } else { //finger touch went left } if (newLocation.y > prevLocation.y) { //finger touch went upwards } else { //finger touch went downwards } }