Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITouch touchesMoved Finger Direction and Speed

How can I get Speed and Direction of finger movements in touchmoved function?

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:

UITouch movement speed detection

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);
}
like image 420
Val Nolav Avatar asked Apr 22 '12 20:04

Val Nolav


1 Answers

why not just the below as a variation on obuseme's response

-(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
         }
}
like image 163
Ben Jerrim Avatar answered Sep 23 '22 06:09

Ben Jerrim