2010-07-26 11 views
6

Así que aquí hay una pregunta extraña. Tengo un UIImageView que responde a touchesMoved: & touchesBegan:, funciona perfectamente. Lo principal con lo que estoy teniendo problemas es si pones el dedo sobre decir imagen1 y luego con ese dedo hacia abajo en la imagen1, presionarás imagen2 con el otro dedo.toquesBegan & touchesMoved

A su vez esto dispararía image1 de nuevo. No quiero que eso suceda. Quiero poder usar Multi Touch al presionar ambas imágenes al mismo tiempo y no dispararlas una y otra vez. Debe haber algo que deba agregar para evitar que eso suceda.

Código:

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

    } 

Y para el 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; 
     } 

    } 

Por último aquí son las finales:

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

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

Así que sólo hay que hacerlo por lo que si estoy manteniendo presionada una de las imágenes y luego presione otra y suéltela de nuevo y vuelva a presionarla para que deje de disparar la primera imagen. Estoy presionado ng abajo.

Respuesta

0

Utilice los toques NSSet que se pasan al método touchesBegan:, en lugar de allTouches: del evento.

+0

¿Cómo lo escribiría? He intentado utilizar: - (void) touchesBegan: (NSSet *) toca withEvent: (UIEvent *) evento { \t UITouch * toque = [[touchesForView evento: self.view] anyObject]; \t \t CGPoint location = [touch locationInView: touch.view]; No creo que eso sea lo que usted quiso decir. Mente escribiéndolo así lo entiendo exactamente. Gracias. – AndrewDK

+0

Uno de los parámetros de ese método es un NSSet * que son los toques que se ven afectados. Por lo tanto, en - (void) touchesBegan: (NSSet *) toques withEvent: (UIEvent *) evento {...} solo use * que * toque la variable. Ese conjunto contendrá el toque (s) que comenzó durante ese evento. - (void) touchesBegan: (NSSet *) toques con Evento: (UIEvent *) evento { UITouch * touch = [toca cualquier Objeto]; // lo que quieras hacer con el toque ... } –

+0

Sí, gracias por aclarar, todavía no funciona. Cuando tengo un dedo hacia abajo sobre una imagen y luego presiono otra, todavía se disparará. También cuando intento presionar 2 imágenes a la vez, disparan y luego el primer golpe disparará nuevamente con ese toque. Es realmente extraño. – AndrewDK

1

Todas las vistas implicadas recibirán todos los toques (en el NSSet como lo mencionó Jason). Depende de cada vista (depende de usted) seleccionar aquellas que le interesen. Por lo tanto, debe verificar si cada toque se encuentra dentro de los límites de la vista e intentar correlacionar el toque con el último recibido. Desde que obtiene los toques en un conjunto, no puede confiar en su orden para calcular cuánto se movieron, por ejemplo. Tienes que guardar las coordenadas del último toque y seleccionar el toque más cercano y asumir que es el mismo dedo que se ha movido.

+0

Un poco confuso, entiendo lo que quieres decir. Tendré que jugar un poco más con eso. – AndrewDK

Cuestiones relacionadas