Skip to content

Instantly share code, notes, and snippets.

@masuhara
Last active February 12, 2019 10:36
Show Gist options
  • Save masuhara/0d8f78c7c5086c3efbb4bf32a1eef745 to your computer and use it in GitHub Desktop.
Save masuhara/0d8f78c7c5086c3efbb4bf32a1eef745 to your computer and use it in GitHub Desktop.
カウントダウンタイマー
【カウントダウンタイマー】
// ①Storyboardにタイマー用のLabelを置く
// ②変数宣言する
// タイマー用カウンター
var count: Double = 0.0
// タイマー用ラベル
@IBOutlet var timerLabel: UILabel!
// ②func showQuizの最後に以下のコードを追加する
Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(countDownTimer(timer:)), userInfo: nil, repeats: true)
// ③タイマーのメソッドをclass ViewControllerの最後のとじカッコの前に追加
func countDownTimer(timer: Timer) {
if count < 0.0 {
timer.invalidate()
count = 5.0
updateQuiz()
} else {
count = count - 0.01
}
timerLabel.text = String(format: "%.2f", count)
}
// ④timerLabelを①で置いたラベルと関連付け
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment