我正在努力学习Swift,创建一个简单的游戏。在一轮结束时,我使用了UIGestureRecognizer
来清除模态/ hudview,一旦清除,它也会清除游戏板。我也想在clearBoard(recognizer: UITapGestureRecognizer)
函数中调用restart()
函数,但由于UITapGestureRecognizer
clearBoard()
参数的必要性而无法调用clearBoard
函数
如何在手势识别器和restart
功能中使用class GameViewController: UIViewController {
// Setup views etc.
func endGame() {
let tapRec = UITapGestureRecognizer()
if winner == 1 {
self.playerScore++
self.playerScoreLabel.text = String(self.playerScore)
let hudView: HudView = HudView.hudInView(self.view, message: "Player won!", animated: true)
tapRec.addTarget(self.view, action: "clearBoard")
self.view.addGestureRecognizer(tapRec)
} else {
self.computerScore++
self.computerScoreLabel.text = String(self.computerScore)
let hudView: HudView = HudView.hudInView(self.view, message: "Computer won!", animated: true)
}
}
func restart() {
// Restart
// Problem here
self.clearBoard()
}
func clearBoard(recognizer: UITapGestureRecognizer) {
// Clear board
}
}
功能?以下简化代码:
{{1}}
答案 0 :(得分:1)
您不必将识别器设置为参数。这只是苹果为您提供的可能性。你也可以不传递这样的参数:
func clearBoard(){
//Clear board
}
它也适用于手势识别器。您不必更改手势代码中的任何内容:
tapRec.addTarget(self.view, action: "clearBoard")
这绝对没问题。