Эффективность кода, многие функции распознавания жестов - PullRequest
0 голосов
/ 27 апреля 2018

Я построил игру на соответствие памяти для своего приложения (см. Рисунок ниже)

Memory game

Каждая карта в игре памяти представляет собой UIImageView. У меня вопрос следующий. Чтобы создать эту игру, я добавил 16 распознавателей жестов, 16 выходов и 16 функций (по 1 на распознаватель жестов). Однако это кажется очень неэффективным. Мой вопрос заключается в том, как сделать этот распознаватель жестов и розетку более простым? Что если бы у меня была сетка 100 на 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()
}
...