swift
1 2 @IBOutlet weak var countdwonTimer: UILabel! 3 4 5 var count = 0 6 var timer : Timer? 7 8 9 @IBAction func startButton(_ sender: Any) { 10 if let nowTimer = timer { 11 if nowTimer.isValid == true { 12 return 13 } 14 } 15 timer = Timer.scheduledTimer(timeInterval: 1.0, 16 selector : # selector(self.timerInterrupt(_:)) 17 userInfo : nil, 18 repeats : true) 19 } 20 21 @IBAction func stopButton(_ sender: Any) { 22 if let nowTimer = timer { 23 if nowTimer.isValid == true { 24 nowTimer.invalidate() 25 } 26 } 27 } 28 func displayUpdate() -> Int { 29 let remainCount = 10 - count 30 countdwonTimer.text = "残り(remainCOunt)" 31 return remainCount 32 } 33 34 @obj func timerInterrupt(_ timer: Timer) { 35 count += 1 36 if displayUpdate() <= 0 { 37 count = 0 38 timer.invalidate() 39 } 40 }
スタートボタンで10秒カウントダウン開始 残り3秒でストップボタンを押でカウントダウンを止め、スタートボタンを押したらまた3秒からカウントダウンが開始されるようにしたい。
fire()を使うのかと思ったのですがfire()の存在意義や使い方がわかりませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/31 05:37
2019/05/31 07:34