当用户触摸屏幕上的特定区域时如何播放声音

时间:2010-10-25 12:48:36

标签: iphone

我正在设计一个由图片组成的应用程序......

我正在尝试使用以下代码播放声音...

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

} 
}

在上面的代码中,当在屏幕上的任何地方检测到触摸时,声音播放。当用户触摸屏幕上的不同位置时,我想播放5种不同的声音(例如1.wav,当用户触摸宽度为100-200,高度为50-100时)。任何人都可以帮我如何设置半径,并在指定的半径内触摸时播放不同的声音...

1 个答案:

答案 0 :(得分: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 ...
......