UITouch触及手指方向和速度 [英] UITouch touchesMoved Finger Direction and Speed
问题描述
如何在touchmoved功能中获得手指移动的速度和方向?
How can I get Speed and Direction of finger movements in touchmoved function?
我想获得手指速度和手指方向并将其应用于UIView类方向移动和动画速度。
I want to get the finger speed and finger direction and apply it in a UIView class direction movement and animation speed.
我读了这个链接,但我无法理解答案,此外它没有解释我如何检测方向:
I read this link but I can not understand the answer, in addition it is not explaining how I can detect the direction:
到目前为止,我尝试了这段代码:
so far, I tried this code:
-(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的值。具体而言,位置将包含新的触摸点。例如:
Direction will be determined from the values of "location" and "prevLocation" in touchesMoved. Specifically, location will contain the new point of the touch. For example:
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的值相比。
Now touchesMoved will get called many times for a given finger movement. It will be key to your code to compare an initial value when the finger first touches the screen, with a value of the CGPoint when the movement is finally complete.
这篇关于UITouch触及手指方向和速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!