ios - 代码效率,许多点击手势识别器
问题描述
我为我的应用程序构建了一个记忆匹配游戏(见下图)
记忆游戏中的每张卡片都是一个 UIImageView。我的问题如下。为了构建这个游戏,我添加了 16 个手势识别器、16 个插座和 16 个函数(每个手势识别器 1 个)。然而,这似乎非常低效。我的问题是,这个手势识别器和插座问题如何变得更简单?如果我有一个 100 x 100 的网格,我需要为每个网格制作 100 个不同的插座和手势识别器吗?这似乎效率低下。我不能在这里发布我的所有代码,但我会发布我的手势识别器,以便您了解我在说什么。
@IBOutlet weak var R1C1: UIImageView!
@IBOutlet weak var R1C2: UIImageView!
@IBOutlet weak var R1C3: UIImageView!
@IBOutlet weak var R1C4: UIImageView!
@IBOutlet weak var R2C1: UIImageView!
@IBOutlet weak var R2C2: UIImageView!
@IBOutlet weak var R2C3: UIImageView!
@IBOutlet weak var R2C4: UIImageView!
@IBOutlet weak var R3C1: UIImageView!
@IBOutlet weak var R3C2: UIImageView!
@IBOutlet weak var R3C3: UIImageView!
@IBOutlet weak var R3C4: UIImageView!
@IBOutlet weak var R4C1: UIImageView!
@IBOutlet weak var R4C2: UIImageView!
@IBOutlet weak var R4C3: UIImageView!
@IBOutlet weak var R4C4: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
loadCards()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
R1C1.addGestureRecognizer(tapGestureRecognizer)
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageTapped2(tapGestureRecognizer:)))
R1C2.addGestureRecognizer(tapGestureRecognizer2)
let tapGestureRecognizer3 = UITapGestureRecognizer(target: self, action: #selector(imageTapped3(tapGestureRecognizer:)))
R1C3.addGestureRecognizer(tapGestureRecognizer3)
let tapGestureRecognizer4 = UITapGestureRecognizer(target: self, action: #selector(imageTapped4(tapGestureRecognizer:)))
R1C4.addGestureRecognizer(tapGestureRecognizer4)
let tapGestureRecognizer5 = UITapGestureRecognizer(target: self, action: #selector(imageTapped5(tapGestureRecognizer:)))
R2C1.addGestureRecognizer(tapGestureRecognizer5)
let tapGestureRecognizer6 = UITapGestureRecognizer(target: self, action: #selector(imageTapped6(tapGestureRecognizer:)))
R2C2.addGestureRecognizer(tapGestureRecognizer6)
let tapGestureRecognizer7 = UITapGestureRecognizer(target: self, action: #selector(imageTapped7(tapGestureRecognizer:)))
R2C3.addGestureRecognizer(tapGestureRecognizer7)
let tapGestureRecognizer8 = UITapGestureRecognizer(target: self, action: #selector(imageTapped8(tapGestureRecognizer:)))
R2C4.addGestureRecognizer(tapGestureRecognizer8)
let tapGestureRecognizer9 = UITapGestureRecognizer(target: self, action: #selector(imageTapped9(tapGestureRecognizer:)))
R3C1.addGestureRecognizer(tapGestureRecognizer9)
let tapGestureRecognizer10 = UITapGestureRecognizer(target: self, action: #selector(imageTapped10(tapGestureRecognizer:)))
R3C2.addGestureRecognizer(tapGestureRecognizer10)
let tapGestureRecognizer11 = UITapGestureRecognizer(target: self, action: #selector(imageTapped11(tapGestureRecognizer:)))
R3C3.addGestureRecognizer(tapGestureRecognizer11)
let tapGestureRecognizer12 = UITapGestureRecognizer(target: self, action: #selector(imageTapped12(tapGestureRecognizer:)))
R3C4.addGestureRecognizer(tapGestureRecognizer12)
let tapGestureRecognizer13 = UITapGestureRecognizer(target: self, action: #selector(imageTapped13(tapGestureRecognizer:)))
R4C1.addGestureRecognizer(tapGestureRecognizer13)
let tapGestureRecognizer14 = UITapGestureRecognizer(target: self, action: #selector(imageTapped14(tapGestureRecognizer:)))
R4C2.addGestureRecognizer(tapGestureRecognizer14)
let tapGestureRecognizer15 = UITapGestureRecognizer(target: self, action: #selector(imageTapped15(tapGestureRecognizer:)))
R4C3.addGestureRecognizer(tapGestureRecognizer15)
let tapGestureRecognizer16 = UITapGestureRecognizer(target: self, action: #selector(imageTapped16(tapGestureRecognizer:)))
R4C4.addGestureRecognizer(tapGestureRecognizer16)
//end viewDidLoad method
}
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R1C1, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[0].image, ofType: "png")
R1C1.image = UIImage(contentsOfFile: imagePath!)
memoryArray[0][0].cardIsRevealed = true
memoryArray[0][0].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[0][0].sound = newItemList[0].image
R1C1.contentMode = .scaleAspectFit
count += 1
R1C1.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped2(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R1C2, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[1].image, ofType: "png")
R1C2.image = UIImage(contentsOfFile: imagePath!)
memoryArray[0][1].cardIsRevealed = true
memoryArray[0][1].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[0][1].sound = newItemList[1].image
R1C2.contentMode = .scaleAspectFit
count += 1
R1C2.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped3(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R1C3, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[2].image, ofType: "png")
R1C3.image = UIImage(contentsOfFile: imagePath!)
memoryArray[0][2].cardIsRevealed = true
memoryArray[0][2].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[0][2].sound = newItemList[2].image
R1C3.contentMode = .scaleAspectFit
count += 1
R1C3.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped4(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R1C4, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[3].image, ofType: "png")
R1C4.image = UIImage(contentsOfFile: imagePath!)
R1C4.contentMode = .scaleAspectFit
memoryArray[0][3].cardIsRevealed = true
memoryArray[0][3].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[0][3].sound = newItemList[3].image
count += 1
R1C4.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped5(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R2C1, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[4].image, ofType: "png")
R2C1.image = UIImage(contentsOfFile: imagePath!)
R2C1.contentMode = .scaleAspectFit
memoryArray[1][0].cardIsRevealed = true
memoryArray[1][0].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[1][0].sound = newItemList[4].image
count += 1
R2C1.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped6(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R2C2, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[5].image, ofType: "png")
R2C2.image = UIImage(contentsOfFile: imagePath!)
R2C2.contentMode = .scaleAspectFit
memoryArray[1][1].cardIsRevealed = true
memoryArray[1][1].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[1][1].sound = newItemList[5].image
count += 1
R2C2.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped7(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R2C3, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[6].image, ofType: "png")
R2C3.image = UIImage(contentsOfFile: imagePath!)
R2C3.contentMode = .scaleAspectFit
memoryArray[1][2].cardIsRevealed = true
memoryArray[1][2].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[1][2].sound = newItemList[6].image
count += 1
R2C3.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped8(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R2C4, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[7].image, ofType: "png")
R2C4.image = UIImage(contentsOfFile: imagePath!)
R2C4.contentMode = .scaleAspectFit
memoryArray[1][3].cardIsRevealed = true
memoryArray[1][3].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[1][3].sound = newItemList[7].image
count += 1
R2C4.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped9(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R3C1, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[8].image, ofType: "png")
R3C1.image = UIImage(contentsOfFile: imagePath!)
R3C1.contentMode = .scaleAspectFit
memoryArray[2][0].cardIsRevealed = true
memoryArray[2][0].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[2][0].sound = newItemList[8].image
count += 1
R3C1.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped10(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R3C2, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[9].image, ofType: "png")
R3C2.image = UIImage(contentsOfFile: imagePath!)
R3C2.contentMode = .scaleAspectFit
memoryArray[2][1].cardIsRevealed = true
memoryArray[2][1].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[2][1].sound = newItemList[9].image
count += 1
R3C2.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped11(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R3C3, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[10].image, ofType: "png")
R3C3.image = UIImage(contentsOfFile: imagePath!)
R3C3.contentMode = .scaleAspectFit
memoryArray[2][2].cardIsRevealed = true
memoryArray[2][2].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[2][2].sound = newItemList[10].image
count += 1
R3C3.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped12(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R3C4, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[11].image, ofType: "png")
R3C4.image = UIImage(contentsOfFile: imagePath!)
R3C4.contentMode = .scaleAspectFit
memoryArray[2][3].cardIsRevealed = true
memoryArray[2][3].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[2][3].sound = newItemList[11].image
count += 1
R3C4.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped13(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R4C1, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[12].image, ofType: "png")
R4C1.image = UIImage(contentsOfFile: imagePath!)
R4C1.contentMode = .scaleAspectFit
memoryArray[3][0].cardIsRevealed = true
memoryArray[3][0].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[3][0].sound = newItemList[12].image
count += 1
R4C1.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped14(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R4C2, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[13].image, ofType: "png")
R4C2.image = UIImage(contentsOfFile: imagePath!)
R4C2.contentMode = .scaleAspectFit
memoryArray[3][1].cardIsRevealed = true
memoryArray[3][1].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[3][1].sound = newItemList[13].image
count += 1
R4C2.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped15(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R4C3, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[14].image, ofType: "png")
R4C3.image = UIImage(contentsOfFile: imagePath!)
R4C3.contentMode = .scaleAspectFit
memoryArray[3][2].cardIsRevealed = true
memoryArray[3][2].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[3][2].sound = newItemList[14].image
count += 1
R4C3.isUserInteractionEnabled = false
twoCardsRevealed()
}
func imageTapped16(tapGestureRecognizer: UITapGestureRecognizer)
{
UIImageView.transition(with: R4C4, duration: 0.3, options: .transitionFlipFromTop, animations: nil, completion: nil)
let imagePath = Bundle.main.path(forResource: newItemList[15].image, ofType: "png")
R4C4.image = UIImage(contentsOfFile: imagePath!)
R4C4.contentMode = .scaleAspectFit
memoryArray[3][3].cardIsRevealed = true
memoryArray[3][3].cardImage = UIImage(contentsOfFile: imagePath!)!
memoryArray[3][3].sound = newItemList[15].image
count += 1
R4C4.isUserInteractionEnabled = false
twoCardsRevealed()
}
解决方案
推荐阅读
- jquery - 链接不会打开引导模式
- javascript - 忽略 VueJs 深入观察中的一些属性
- css - 使用css一次生成两个不同的内容
- c# - 使用 c# api 读取和导出多个 revit 文件
- javascript - 使用 PHP 通过电子邮件发送动态下载字段
- javascript - 如何使用对象 ID 数组在另一个数组中查找这些对象?必须根据给定的 Id 数组找到所有对象
- scala - 如何使用`flatMap`实现延迟计算的成本估算?
- java - 从地图中的对象中删除某些元素
- javascript - 在 C# 中删除一个字符
- mysql - How can I reinstall mysql while restoring DBs without mysqldump?