ios - 从 4 个带有图像的自定义按钮中选择一个按钮

标签 ios objective-c uiimageview uibutton arc4random

我在 4 个按钮中有 4 个图像:imag01.jpgimag02.jpgimag03.jpgimag04.jpg 和 4 个声音:sound01.aifcsound02.aifcsound03.aifcsound04.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/

相关文章:

ios - UISwipeGestureRecognizers 不会被同时识别

ios - 无法将 PrototypeCell 添加到 xib 文件中的 UITableView

ios - 自定义 UITableViewCell 中的 UIImageview 未正确显示

ios - 自定义 UITableViewCell 的 UIImageView 在单击之前不会宽高比

ios - 如何在不在 ViewController 类中时以编程方式更改 View Controller

ios - CallState 永远不会是 CTCallStateDisconnected?

objective-c - 如何快速获取应用程序终止通知 cocoa

ios - 由于 __NSFastEnumerationMutationHandler 异常导致奇怪的崩溃

ios - 从 iOS swift 中的字典中删除值

swift - 如何根据图像调整 UIImaveView 的大小?