拳击计数/延迟/计时器

时间:2017-10-03 04:15:32

标签: swift swift3 delay

我创建了一个拳击模拟,我现在回去添加动画。现在,我正在添加一个" Ref' Countup"添加悬念。我尝试了很多东西,但都没有得到我想要的东西。我试过了:

Thread.sleep(forTimeInterval: 1)
knockdownNumber.text = "\(knockdownNumberInt)"

sleep(1)
knockdownNumber.text = "\(knockdownNumberInt)"

那些暂停了用户界面。所以他们确实等待了适当的时间,但没有改变我的文字。 然后有这些只计算一次(即1,3或1,8)。

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        self.knockdownNumber.text = "\(self.knockdownNumberInt)"
    }

let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
        self.knockdownNumber.text = "\(self.knockdownNumberInt)"
    }

 delay(1) {
        self.knockdownNumber.text = "\(self.knockdownNumberInt)"
    }

延迟功能为

 func delay(_ delay:Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(
        deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}

我调用该函数的方式(这些都是这些)是在他被击倒之后。这段代码看起来像这样。

knockdownNumber.text = "1"
    knockdownNumberInt = 1
    downSec()
    knockdownNumberInt = knockdownNumberInt + 1
    downSec()
    knockdownNumberInt = knockdownNumberInt + 1
    downSec()
    if fighter2.endurance < 400 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }
    if fighter2.endurance < 300 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }
    if fighter2.endurance < 200 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }
    if fighter2.endurance < 100 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }
    if fighter2.endurance < 0 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }
    if fighter2.endurance < -100 {
        knockdownNumberInt = knockdownNumberInt + 1
        downSec()
    }

我真的很难过。

1 个答案:

答案 0 :(得分:1)

你几乎就是这种方法:

let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
    self.knockdownNumber.text = "\(self.knockdownNumberInt)"
}

您需要做的只是将repeats设置为true,计时器将永远重复。

现在我们需要决定何时停止计时器。我们应该在开始计时之前决定这一点,而不是做你做的事情 - 在开始新的时候减少knockdownNumberInt。这不起作用,因为你在计时器关闭中放入的东西是异步的。

所以我们决定我们要计算的数字:

knockdownNumber.text = "1"
knockdownNumberInt = 3
if fighter2.endurance < 400 {
    knockdownNumberInt += 1 // use the "+=" to make it cleaner!
}
if fighter2.endurance < 300 {
    knockdownNumberInt += 1
}
if fighter2.endurance < 200 {
    knockdownNumberInt += 1
}
if fighter2.endurance < 100 {
    knockdownNumberInt += 1
}
if fighter2.endurance < 0 {
    knockdownNumberInt += 1
}
if fighter2.endurance < -100 {
    knockdownNumberInt += 1
}

在类中声明一个名为currentCountDown的属性:

var currentCountDown = 1

然后您可以像这样创建计时器:

let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] (timer) in
    guard let `self` = self else { // I added this to prevent a retain cycle.
         timer.invalidate()
         return
    }
    self.currentCountDown += 1
    self.knockdownNumber.text = "\(self.currentCountDown)"
    if self.currentCountDown == self.knockdownNumberInt {
        timer.invalidate()
    }
}