我正在尝试制作一套!我需要从 UI
中选择 3 张卡片并在它们之间进行比较的游戏(我正在比较图像的名称)例如:
UIButton
1 带图像名称:“1123”
UIButton
2 带图像名称:“1223”
UIButton
3 带图像名称:“1133”
使用这个函数:
func checkingIfMatch(cardOne : String, cardTwo : String, cardThree : String) -> Bool {
let first = cardOne.compactMap{Int(String($0))} //making an array from the UIImage file name
let second = cardTwo.compactMap{Int(String($0))}
let third = cardThree.compactMap{Int(String($0))}
if (first[0] == second[0] && second[0] == third[0]) || (first[0] != second[0] && second[0] != third[0]) {
if (first[1] == second[1] && second[1] == third[1]) || (first[1] != second[1] && second[1] != third[1]) {
if (first[2] == second[2] && second[2] == third[2]) || (first[2] != second[2] && second[2] != third[2]) {
if (first[3] == second[3] && second[3] == third[3]) || (first[3] != second[3] && second[3] != third[3]) {
print("match!")
return true
}
}
}
}
print("no match!")
return false
}
我试图在点击按钮时获取 UIButton
图像的名称以执行此操作,但似乎无法找到执行此操作的方法。
任何帮助将不胜感激,如果没有办法做到这一点,我想听听不同的想法。
最佳答案
我不知道你想要达到什么目的,但你可以使用 tag
获取点击的按钮。
在 Storyboard 中,将您的三个按钮连接到相同的 @IBAction
,sender
类型为 UIButton
,然后只需围绕标签区分大小写即可知道用户点击了哪个按钮。
@IBAction func tappedButton(_ sender: UIButton) {
switch sender.tag {
case 0: // image One
case 1: // image Two
case 2: // image Three
default: // default condition where there is no matching cases
}
}
只需将按钮拖到功能上, 不要忘记给他们标签。
btn.tag = 0
或直接来自 IB。
关于ios - 有没有办法获取 UIImage 文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849796/