I'm trying to improve my swift coding.
I have a fully working if-else nested function which I believe could be better optimised with a Switch Statement. Can anybody advise the correct way to do this code-refactoring ?
Here's my working code:
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()}
}
}