Xcode 重复该功能

Xcode 重复该功能,xcode,function,loops,timer,swift3,Xcode,Function,Loops,Timer,Swift3,我试图重复这个函数,所以它会一直重复,直到有人在接口上停止它,但当它返回到increaseTimer时,它会出现一个错误:线程1:EXC_BAD_ACCESS。请有人帮助,使这个功能继续自动循环 func increaseTimer() { time += 1 if time > 2 && time < 4 { timerLabel.text = "Hold" } else if (time > 5 &&

我试图重复这个函数,所以它会一直重复,直到有人在接口上停止它,但当它返回到increaseTimer时,它会出现一个错误:线程1:EXC_BAD_ACCESS。请有人帮助,使这个功能继续自动循环

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}

不要试图无限重复代码来模拟计时器。相反,使用Timer对象。下面的代码将启动计时器,并每隔1秒调用提供的闭包

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { timer in
    increaseTimer()
})
如果要停止计时器,请调用invalidate

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
要在消息之间切换,请执行以下操作:

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
func updateMessage() {

    time += 1

    switch time {
    case 0 ... 5:
        timerLabel.text = "Hold"
    case 6 ... 10:
        timerLabel.text = "Breathe Out"
    default:
        time = 0
    }

}

每次需要重复代码块时,都可以重置时间Int。如下所示:

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
首先,我要声明我的计时器,并将其初始值设置为0

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
var timer : Timer = 0
然后,将以下内容放在您想要启动计时器的任何位置。在@iAction中或任何地方

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateMessage), userInfo: nil, repeats: true)
然后在switch语句中放置重置时间变量的代码。我会将它设置为1,因为这是在第11秒

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
func updateMessage() {

    time += 1

    switch time {
    case 1 ... 5:
        timerLabel.text = "Hold"
    case 6 ... 10:
        timerLabel.text = "Breathe Out"
        time = 0
    case 11:
        timerLabel.text = "Hold"
//Reset the time Int to start the loop again
        time = 1
    default:
        break
    }
}
当你不再需要计时器时,一定要使它失效

    func increaseTimer() {
    time += 1

    if time > 2 && time < 4 {
        timerLabel.text = "Hold"
    } else if (time > 5 && time < 10) {
        timerLabel.text = "Breathe out"
    } else if (time > 11 && time < 14) {
        timerLabel.text = "Hold"
    } else { return increaseTimer()}
timer.invalidate()

您的方法不应该返回任何内容,您正在尝试返回该方法本身。谢谢,这是我在计时器部分处理的部分代码。我试图更改它,使其与上面的注释类似,但在重复时遇到问题:{timer1=Timer.scheduledTimertimeInterval:1,target:self,selector:selectorViewController3.increaseTimer,userInfo:nil,repeats:true}看起来应该可以工作。您是否修改了increaseTimer函数,使其不向自身返回调用?是的,我修改了该函数,它可以工作,但我尝试的是获取它,以便timerlabel.text自动重复整个函数,并且不会停止。你知道我会怎么做吗?谢谢。我不知道你的意思。似乎您希望在经过一定的秒数后更改标签,这是我的解决方案将要做的,因为它将每秒调用您的更新代码。这是正确的,但它不会在之后循环:否则,如果时间>11&&time<14{timerLabel.text=Hold}