2010-10-25 3 views
0

Je suis la conception d'une application qui se compose d'une image ...Comment jouer un son lorsque l'utilisateur touche une zone particulière de l'écran

Je suis en train de jouer son en utilisant le code suivant ...

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

UITouch *touch = [touches anyObject]; 


CGPoint point = [touch locationInView:self.view]; 
if (CGRectContainsPoint(CGRectMake(320,480,0,0),point));//CGRectMake(5, 5, 40, 130), point)) 
{ 
    AVAudioPlayer *player; 
    if(!player){ 

     NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; 
     resourcePath = [resourcePath stringByAppendingString:@"/try.wav"]; 
     NSLog(@"Path to play: %@", resourcePath); 
     NSError* err; 

     //Initialize our player pointing to the path to our resource 
     player = [[AVAudioPlayer alloc] initWithContentsOfURL: 
        [NSURL fileURLWithPath:resourcePath] error:&err]; 

     if(err){ 
      //bail! 
      NSLog(@"Failed with reason: %@", [err localizedDescription]); 
     } 
     else{ 
      //set our delegate and begin playback 
      player.delegate = self; 
      [player play]; 
     } 
    } 

} 
} 

Dans le code ci-dessus, le son est émis lorsque le toucher est détecté n'importe où sur l'écran. Je veux jouer 5 sons différents lorsque l'utilisateur touche un endroit différent sur l'écran (disons 1.wav lorsque l'utilisateur touche entre 100-200 en largeur et 50-100 en hauteur). Quelqu'un peut-il m'aider s'il vous plaît comment définir le rayon et aussi de jouer différents sons lorsqu'il est touché dans le rayon spécifié ...

Répondre

2
CGRect area1 = CGRectMake(10,10, 10, 10); 
CGRect area2 = CGRectMake(100,10, 10, 10); 
CGRect area3 = CGRectMake(10,100, 10, 10); 
CGRect area4 = CGRectMake(100,100, 10, 10); 

if (CGRectContainsPoint(area1, point)) { 
// play sound 1 
} else ... 
...... 
+0

Merci pour la réponse rapide ... –