touchesBegan & touchesMoved

Alors, voici une question étrange. J'ai une UIImageView qui répond à touchesMoved: & touchesBegan:, il fonctionne parfaitement. La principale chose que je vais avoir un problème c'est que si vous mettez votre doigt vers le bas sur le dire image1 et puis avec ce doigt vers le bas sur image1 vous appuyez sur image2 avec vos autres doigts.

À son tour, ce serait le feu image1 à nouveau. Ne veux pas que cela se produise. Je veux être en mesure d'utiliser Multi-Touch en appuyant sur les deux images en même temps et pas de tir les plus et plus. Il doit y avoir quelque chose que je dois ajouter pour empêcher ça.

Code:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches]; 
    for (UITouch *touch in allTouches) 
    { 
        CGPoint location = [touch locationInView:touch.view];
        if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image1;
        }
        if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image2;
        }

    }

Et pour le touchesMoved:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
        if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image1;
        }
        if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) {
            //Play Sound
            NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" 
                                                             ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path]
                                             , &soundID);
            AudioServicesPlaySystemSound (soundID); 
            //
            lastButton = image2;
        }

    }

Enfin voici la finale:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    lastButton = nil;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

Donc j'ai juste besoin de le faire donc si je suis la maintenant enfoncée l'une des images, puis appuyez sur un autre bouton et relâchez-le de nouveau et de le pousser encore qu'il s'arrête de tir à la première image, je suis en appuyant sur.

InformationsquelleAutor AndrewDK | 2010-07-26