9秒に1ずつ増え、ラベルに8が表示されたらインターバルを8秒にしたいのですがこのコードだとタイマーにズレが生じます。どこが間違っているのか教えていただきたいです。
Swift
1timer1 = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in 2 3 //カウントダウン 4 self.count = self.count - 1 5 6 //ラベルに反映 7 self.countLabel.text = String(self.count) 8 9 if (self.count == 0) { 10 11 //タイマー1ストップ 12 self.timer1.invalidate() 13 self.timer2 = Timer.scheduledTimer(withTimeInterval: 9.0, repeats: true, block: { (timer) in 14 15 //9秒ごとにカウントアップ 16 self.count = self.count + 1 17 self.countLabel.text = String(self.count) 18 }) 19 20 21 } 22 23 if (self.count == 8) { 24 25 //タイマー1ストップ 26 self.timer2.invalidate() 27 self.timer3 = Timer.scheduledTimer(withTimeInterval: 8.0, repeats: true, block: { (timer) in 28 29 //8秒ごとにカウントアップ 30 self.count = self.count + 1 31 self.countLabel.text = String(self.count) 32 }) 33 34
回答2件
あなたの回答
tips
プレビュー