-1

我制作了一个应用程序来玩弄它,它基本上是一个飞扬的鸟克隆。我需要知道如何在触摸屏幕时发出声音。我尝试使用按钮,但声音只会在触摸按钮时播放,而不是整个屏幕。当我按下按钮时,那只鸟就掉了下来。我知道我可能必须使用 TouchBegan,但是我使用什么代码以及将它们放在哪里?越详细越好,因为我在这方面是个大菜鸟。谢谢!

4

2 回答 2

1

如果您使用的是UIView子类,则可以实现以下方法来确定是否发生了点击。从那里,只需播放您的声音。(注意,使用 ARC)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // now that we know that a touch has occurred, lets play a sound
    NSString *pathToMySound = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"aif"];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID([NSURL fileURLWithPath: pathToMySound], &soundID);
    AudioServicesPlaySystemSound(soundID);
}
于 2014-04-04T04:29:00.403 回答
0

尝试将 添加UITapGestureRecogniser到可能是 的主视图中self.view,或者您所命名的任何内容。

确保启用了用户交互,并创建一个选择器,每次调用时都会播放声音。

这篇文章可能会对你有所帮助,我还没有尝试过,但看起来它朝着正确的方向发展。我会以编程方式添加 UIGesture。

--这就是你如何创建一个UITapGesture

//this stuff goes  in your viewDidLoad for example
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSthCool:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer: singleTap]; //self or wherever you want to add it

.....
.....

- (void) doSthCool: (UITapGestureRecognizer *)recognizer{
     //Code to handle the gesture
     NSLog(@"flap");
}

此外,在尝试使用 URL 时,如果您不知道它在做什么,请不要使用 CFURLRef,而是尝试以下操作:

NSURL *path = [NSURL URLWithString:@"whatever the url/path is."];

我建议你在遇到困难时谷歌一些东西,这是最好的学习方式:)

祝你好运!玩得开心编码:)

于 2014-04-04T04:18:50.087 回答