【问题标题】:iPhone - How to play a sound on button touch?iPhone - 如何在按钮触摸时播放声音?
【发布时间】:2010-01-17 01:27:42
【问题描述】:

我正在尝试让按钮在触摸按钮时播放声音。我可以使用 Touch Up Inside 选项播放声音,但这不是我想要的,因为只有在释放按钮后才会播放声音。

我尝试使用 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];

    }

}

【问题讨论】:

  • 你想像钢琴应用一样做吗?当用户按住它时,声音会继续播放
  • 您知道,您可以使用 UIButton,并执行除内部修饰之外的其他操作,例如“Touch Down”
  • Touch Down 有效,但我不希望按钮触发我的模态视图,直到按钮被抬起。不,我不是在做钢琴应用程序,哈哈……那里的应用程序太多了。这只是我的喜好。
  • 那么为什么不在 touch down 时播放声音并在 touch up 时触发模态视图呢?

标签: iphone objective-c iphone-sdk-3.0 uibutton


【解决方案1】:

使用UIButton。触摸时致电-play,触摸时致电-stop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2015-05-01
    相关资源
    最近更新 更多