2010-06-27 5 views
0

Je suis assez nouveau en programmation et j'ai un problème en ce moment pour trouver des méthodes sur mon programme. de toute façon je ne peux pas comprendre comment travailler sur mon événement touchmove. Je veux déplacer une image dans la direction x ou y en fonction de la direction de l'événement touchmove mais je veux que le mouvement soit dans un mouvement de type grille de 20 pixels. J'utilise Touchbegan pour vérifier quelle image est tactile;Déplacer UIImage par touchmove par incrément de 20 pixels X-coord ou Y-coord

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:touch.view]; 

if(CGRectContainsPoint(image1.frame,location)) 
    image1IsDragging = YES; 
else 
    image2IsDragging=NO; 

}

et je suis coincé avec mon événement touchmoved.

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:touch.view]; 

    if (image1IsDragging == YES) { 
    //stuck here on how to check the direction of the swipe and how to move in increment of 20 pixs 
    } 

}

Je pensais obtenir l'arrondi de la distance comme;

float distanceX = newLocation.x - firstTouch.x; 

mais je ne sais pas comment le faire. J'espère que quelqu'un pourrait me prêter de l'aide. Merci.

+0

Comment vous déplacez le UIImage? Êtes-vous en train de déplacer un UIImageView, ou mettez-vous à jour l'affichage avec drawRect ...? – daltonclaybrook

Répondre

0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    **CGPoint pastLocation = [touch previousLocationInView:self.view];** //previous touch 


if (image1IsDragging == YES) { 

     **if(pastLocation.x <location.x) //check the direction for x 
    image.center =CGPointMake(yourimage.center.x +20, image.center.y); 
    else 
     image.center =CGPointMake(image.center.x -20, image.center.y);** 



      } 
    } 
+0

Une petite explication aiderait –

Questions connexes