arrays - 每次单击按钮时如何获取不同的 UIImage 数组?

标签 arrays swift

所以基本上我有一个按钮,每次单击该按钮时,我需要将 UIImage 随机更改为数组中的三个图像之一。现在,当我单击按钮时会发生什么,它只是选择一个随机图像,然后当我再次单击它时,图像保持不变。

这是我在按钮内写的内容:

@IBAction func scissorButton(_ sender: UIButton) {
    playerChoice.image = scissor.png
    computerChoice.image = computerArray[randomChoice]
}

最佳答案

大概您生成了一次随机数并将结果存储在randomChoice中。但每次点击按钮时,您都需要生成一个新的随机数。

最简单的选择是:

@IBAction func scissorButton(_ sender: UIButton) {
    computerChoice.image = computerArray.randomElement()
}

关于arrays - 每次单击按钮时如何获取不同的 UIImage 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55859624/

相关文章:

C++ 矩阵到动态二维数组

ios - Swift 中的 MKTileoverlay 问题

iOS swift 为什么两种类型的闭包不能添加到数组

swift - 一页中的两个collectionView具有不同的数据

c - 在 C 中逐行读取 X&Y 坐标并将它们存储在不同的数组中

c - 打印结构字段多次只打印最后一个元素 - C

c - 段错误: Core Dumped while initializing large size arrays

ios - 使用 SpriteKit 创建 2D 液体

ios - 交互式 Pop Gesture 的安全区域底部布局问题

arrays - 在 PL/pgSQL 中循环遍历给定的值列表