2012-04-22 13 views
11

Come posso ottenere la velocità e la direzione dei movimenti delle dita nella funzione touchmoved?UITouch tocca Direzione e velocità del dito spostate

Voglio ottenere la velocità del dito e la direzione del dito e applicarlo in un movimento di direzione della classe UIView e velocità di animazione.

ho letto questo link, ma non riesco a capire la risposta, in aggiunta non è spiegare come posso rilevare la direzione:

UITouch movement speed detection

finora, ho provato questo codice:

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

risposta

17

La direzione verrà determinata dai valori di "posizione" e "prevLocation" in Toccati. In particolare, la posizione conterrà il nuovo punto del tocco. Ad esempio:

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 
} 

Ora i Tocchi spostati vengono richiamati più volte per un dato movimento del dito. Sarà fondamentale per il tuo codice confrontare un valore iniziale quando il dito tocca per la prima volta lo schermo, con un valore del CGPoint quando il movimento è finalmente completato.

+0

si prega di controllare questo, sto anche paragonando i tocchi, ma il suo divenire lento http://stackoverflow.com/questions/21952274/how- to-effective-process-uitouches-in-a-multitouch-sequence – Ranjit

+0

Si prega di modificare il commento sulla direzione su e giù, sono opposti a – Garnik

5

perché non solo il seguito come una variazione sul risposta di 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

non dimenticare '[super touchesMoved: touch withEvent: event]'! : D – taber

Problemi correlati