Я пытаюсь улучшить свое быстрое кодирование.
У меня есть полностью работающая вложенная функция if-else, которую, я считаю, можно было бы лучше оптимизировать с помощью оператора Switch. Кто-нибудь может посоветовать правильный способ сделать этот рефакторинг кода?
Вот мой рабочий код:
if firstButton.frame.contains(location) {
UIView.animate(withDuration: 0.4) {
self.firstButton.transform = CGAffineTransform(scaleX: -1, y: 1)
self.firstButton.alpha = 0
if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.firstButton.text!, duration: 0.3);self.lastLetterTapped()}
}
} else
if secondButton.frame.contains(location) {
UIView.animate(withDuration: 0.4) {
self.secondButton.transform = CGAffineTransform(scaleX: -1, y: 1)
self.secondButton.alpha = 0
if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.secondButton.text!, duration: 0.3);self.lastLetterTapped()}
}
} else
if thirdButton.frame.contains(location) {
UIView.animate(withDuration: 0.4) {
self.thirdButton.transform = CGAffineTransform(scaleX: -1, y: 1)
self.thirdButton.alpha = 0
if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.thirdButton.text!, duration: 0.3);self.lastLetterTapped()}
}
} else
if fourthButton.frame.contains(location) {
UIView.animate(withDuration: 0.4) {
self.fourthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
self.fourthButton.alpha = 0
if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fourthButton.text!, duration: 0.3);self.lastLetterTapped()}
}
} else
if fifthButton.frame.contains(location) {
UIView.animate(withDuration: 0.4) {
self.fifthButton.transform = CGAffineTransform(scaleX: -1, y: 1)
self.fifthButton.alpha = 0
if self.tapCount == 0 {self.placeholderText1.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 1 {self.placeholderText2.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 2 {self.placeholderText3.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 3 {self.placeholderText4.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.tapCount += 1}
else if self.tapCount == 4 {self.placeholderText5.textWithAnimation(text: self.fifthButton.text!, duration: 0.3);self.lastLetterTapped()}
}
}