2010-01-17 47 views
0

我試圖讓按鈕在按下按鈕時播放聲音。我可以使用Touch Up Inside選項播放聲音,但這不是我要找的,因爲聲音只在按鈕釋放後播放。iPhone - 如何播放按鈕觸摸上的聲音?

我試着用touchesBegan在觸摸按鈕時播放聲音,但它似乎不工作。任何想法爲什麼?

感謝

我的代碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
[super touchesBegan:touches withEvent:event]; 
    if([touch view] == doneButton) { 


NSString *path = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"caf"]; 
AVAudioPlayer* clickAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

clickAudio.delegate=self; 
[clickAudio play]; 

    } 

}

+0

你喜歡鋼琴應用嗎?當用戶按住它時,聲音繼續播放 – 2010-01-17 02:12:20

+0

您知道,您可以使用UIButton,並在其他位置進行其他操作,比如「Touch Down」 – 2010-01-17 02:13:30

+0

Touch Down作品但我不想按鈕來觸發我的模態視圖,直到按鈕被解除。不,我沒有做一個鋼琴應用程序,哈哈......太多了。這只是我的偏好。 – 0SX 2010-01-17 02:53:42

回答

1

使用UIButton。觸摸時撥打-play,然後撥打-stop