multiTouch和音频

时间:2012-05-08 03:57:53

标签: ios multi-touch

我有一些多点触控问题

这是我在.m文件中的代码的一部分

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self.view];

if(pt.x>634 && pt.x<733 && pt.y >64 && pt.y<145)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"si" ofType:@"mp3"];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

if(pt.x>634 && pt.x<733 && pt.y >195 && pt.y<276)
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"la" ofType:@"mp3"];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}


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

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"rest" ofType:@"mp3"];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

我希望这可行。

实际上,第二个“if((范围)&amp;&amp;(范围))”部分不起作用。谁能告诉我解决方案?

第一个范围有效,我希望第二个范围播放“la.mp3”,但当两个手指触地时,它不播放任何音乐。

2 个答案:

答案 0 :(得分:1)

抱歉,这将是对答案的完整编辑,因为我无法删除最后一个。我做了一个重大的阅读错误,我需要写一个新的答案。您不能使用[touches anyObject],因为这无法在多点触控系统中为您提供所需的触摸效果。

您必须使用[[touches allObjects] objectAtIndex:#]来提取触摸。接触将按照他们来的顺序进行。

答案 1 :(得分:0)

您需要重新阅读MultiTouch Events上的文档。首先,您需要将视图的multipleTouchEnabled属性设置为YES。其次,当你设置

时,你每次只能获得一次触摸事件
UITouch *touch = [touches anyObject];   

而是尝试

for (UITouch * touch in touches) {

在当前集合中的每次触摸时执行代码。

其次,我担心你的AVAudioPlayer将在TouchDidBegin终止后发布;我认为这会取消其余的播放。它应该是一个财产。此外,AVAudioPlayer一次只能播放一个音轨,所以你想要两个不同的音轨。

另一种(更好的)替代方案是完全放弃触摸并只使用按钮。当按下按钮(开始音符)和释放按钮时,可以通知您(跟踪哪些按钮仍然按下,如果您在最后一个按下,则播放“rest.mp3”) )。