【发布时间】:2014-03-05 16:48:26
【问题描述】:
我正在尝试使用音板应用程序,并且我的视图控制器上需要超过 50 个按钮(每个声音一个按钮)。此外,我想完全以编程方式制作按钮(和音频播放器代码),因为在故事板和 .h/.m 文件之间不断切换有点烦人。我没有复制和粘贴相同的按钮代码 50 次,而是使用这个 for 循环为我制作按钮:
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=100;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:@selector(playAudioMethod) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:aButton];
yCoord += buttonHeight + buffer;
}
[scrollView setContentSize:CGSizeMake(700, yCoord)];
按钮单击事件选择器中的 playAudioMethod 只播放一种声音。怎么会有50个声音,每个对应一个单独的按钮?抱歉,如果这是一个非常基本的问题,我仍在学习目标 C。谢谢!
编辑:
这是 playAudioMethod:
- (void) playButtonSound:(id)inSender
{
AudioServicesPlaySystemSound(self.SoundID);
}
它基本上只是播放 SoundID 属性,我通过这个传递了一个声音文件:
NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"medic_taunts01" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
self.SoundID = SoundID;
【问题讨论】:
-
显示你的 playAudioMethod 方法
-
完成,我刚刚添加了播放音频方法。
标签: ios objective-c audio