我在 4 个按钮中有 4 个图像:imag01.jpg
、imag02.jpg
、imag03.jpg
、imag04.jpg
和 4 个声音:sound01.aifc
、sound02.aifc
、sound03.aifc
、sound04.aifc
>.
我正在生成 4 个这样的按钮:
UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[oneButton setTitle:@"1" forState:UIControlStateNormal];
[oneButton setTitle:@"1" forState:UIControlStateHighlighted];
[oneButton setImage:[UIImage imageNamed:@"imag01.jpg"] forState:UIControlStateNormal];
[oneButton setImage:[UIImage imageNamed:@"imag01.jpg"] forState:UIControlStateHighlighted];
oneButton.frame = CGRectMake(20, 100, 140, 100);
[scrMain addSubview:oneButton];
我想生成一个从 1 到 4 的随机数(例如 3),播放与之相关的声音 (sound03.aifc
) 并要求用户按下正确的按钮声音 (imag03.jpg
)。
如何在按钮和声音之间建立链接?
最佳答案
这样做:
为每个按钮的
tag
属性分配 0 到 3 之间的不同值。将声音放入数组中。
生成 0 到 3 之间的随机整数(含 0 和 3)。使用它作为索引从数组中检索声音。播放该声音。
启用 4 个按钮并要求用户点击相应的按钮。
如果按钮的
tag
属性与您选择的数字匹配,万岁!干得好,用户!否则,太糟糕了!再试一次。
关于ios - 从 4 个带有图像的自定义按钮中选择一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107761/