So basically I want to create a keyboard and when the user click on a letter something should happen:
@IBAction func letterBtn(sender: UIButton) { // All the letter buttons are linked to this func.
switch sender.currentTitle! {
case "A":
moveLetters(sender)
case "B":
moveLetters(sender)
case "C":
moveLetters(sender)
case "D":
moveLetters(sender)
case "E":
moveLetters(sender)
case "F":
moveLetters(sender)
case "G":
moveLetters(sender)
default :
println("Error")
}
}
func animateLetter (pos: UILabel, btn: UIButton) { // Make the letter move towards a label.
UIView.animateWithDuration(0.5, animations: { () -> Void in
btn.center = pos.center
})
}
func moveLetters (btn: UIButton) { // Determine which label the pressed letter should move towards.
switch emptyPos.count {
case 1:
animateLetter(pos1, btn: btn)
emptyPos.append(0)
case 2:
animateLetter(pos2, btn: btn)
emptyPos.append(0)
case 3:
animateLetter(pos3, btn: btn)
emptyPos.append(0)
case 4:
animateLetter(pos4, btn: btn)
emptyPos.append(0)
default:
println("Error")
}
}
I found myself using multiple switch cases that basically do the same thing in 2 different functions, and I was wondering if there a better way than using 26 cases for the entire alphabet, and also for my other function.