swift中的2个十进制计时器
我正在做一个有计时器的游戏。我让数字以0.01秒的间隔递增,但出现的数字中没有小数点。我怎样把这个小数点加进去?计时器运行1秒后得到的结果是“100”而不是“1.00” 以下是我使用的代码:swift中的2个十进制计时器,swift,timer,Swift,Timer,我正在做一个有计时器的游戏。我让数字以0.01秒的间隔递增,但出现的数字中没有小数点。我怎样把这个小数点加进去?计时器运行1秒后得到的结果是“100”而不是“1.00” 以下是我使用的代码: //timer { var timer = NSTimer() var counter = 0 countingLabel.text = String(counter) timer = NSTimer.scheduledTimerWithTimeInterval(1, ta
//timer
{
var timer = NSTimer()
var counter = 0
countingLabel.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
//------timer update-----
func updateCounter() {
countingLabel.text = String(counter++)
if score.text == "10" {
timer.invalidate()
}
}
试着替换
countingLabel.text = String(counter++)
与
非常感谢,100%有效。只是出于兴趣,这种方法不允许生成小数点后3位的数字,是吗?你可以将计数器除以任何你喜欢的数字,包括1000,但我认为,如果你试图增加那么一点点,NSTimer的速度不足以每0.001秒触发一次。@PeterGaffney你不应该用计时器来测量时间。保存开始日期并获取时间间隔现在仅使用计时器更新用户界面。你不需要超过1/30秒。
counter++
countingLabel.text = String(Double(counter)/100)