所以基本上我想創建一個鍵盤和當在信的東西,用戶點擊應該發生:xcode-幾個案例的重構開關。
@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")
}
}
我發現自己使用多聯開關的情況下,基本上做同樣的事情在2層不同的功能,我是想知道是否有更好的方法比使用整個字母表的26個案例,以及我的其他功能。
太感謝你了,這是非常有幫助:) – Abdou023