2012-04-22 7 views
11

¿Cómo puedo obtener la velocidad y la dirección de los movimientos de los dedos en la función de movimiento táctil?UITouch toca Dirección y velocidad del dedo movido

Quiero obtener la velocidad del dedo y la dirección del dedo y aplicarlo en un movimiento de dirección de clase UIView y la velocidad de animación.

leí este enlace, pero no puedo entender la respuesta, además no está explicando cómo puedo detectar la dirección:

UITouch movement speed detection

hasta ahora, yo probamos este código:

-(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); 
} 

Respuesta

17

La dirección se determinará a partir de los valores de "ubicación" y "ubicación previa" en touchesMoved. Específicamente, la ubicación contendrá el nuevo punto del toque. Por ejemplo:

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 
} 

Ahora touchesMoved obtendrá llamado muchas veces para un determinado movimiento de los dedos. Será clave para su código comparar un valor inicial cuando el dedo toque la pantalla, con un valor de CGPoint cuando el movimiento finalmente se complete.

+0

favor compruebe esto, yo también estoy comparando los tactos, pero su devenir lento http://stackoverflow.com/questions/21952274/how- to-effective-process-uitouches-in-a-multitouch-sequence – Ranjit

+0

Por favor, edite el comentario sobre la dirección arriba y abajo, están opuestos a – Garnik

5

por qué no sólo la continuación como una variación en la respuesta del 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 
     } 
} 
+0

, no olvide '' super touchesMoved: touques withEvent: event] '! :RE – taber

Cuestiones relacionadas