如何当用户触摸屏幕时
问题描述:
我设计其中包括图像的应用程序在一个特定的区域播放声音...如何当用户触摸屏幕时
我将尝试使用下面的代码来播放声音...
- (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种不同的声音(例如,当用户触摸宽度100-200和高度50-100时,1.wav)。任何人都可以请帮助我如何设置半径,并在指定半径内触摸时发出不同的声音...
答
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 ...
......
感谢您的快速回复... – 2010-10-26 06:58:50