如何在第一个函数完成运行时调用函数中的多个函数?

时间:2016-08-02 15:03:44

标签: ios swift xcode

**这是我的简单流程。我仍然需要在#2完成运行后完成调用decisionMaker()时间为up并将测量的全局变量带到decisionmaker()进行案例测试 **

/ *   TestButtonTAPPED()并调用:

1. recordTimer()
2. gatherInput()
 2a. selector: levelTimerCallback()

3. decisionMaker()

* /

// 1.
func recordTimer() {
    /* After 10 seconds, let's stop the recording process */
    let delayInSeconds = 10.0
    let delayInNanoSeconds = DispatchTime.now() + Double(Int64(delayInSeconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)

    DispatchQueue.main.after(when: delayInNanoSeconds, execute: {
        self.soundRecorder!.stop()
        self.handBtn.isHidden = false
    })

}

// 2.
func gatherInput() {
    levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector: #selector(DBListener.levelTimerCallback), userInfo:nil, repeats: true)
}


 func levelTimerCallback() {
    if soundRecorder.averagePower(forChannel: 0) > -30
    {
        // Do gathering for vaiables
    }
}


// 3.
func decisionMaker() {
    // case statments here for final measurement
}

}

1 个答案:

答案 0 :(得分:0)

如果我的想法正确,我可能会有一个解决方案。

 var callbacks: [() -> ()] = []

 var levelTimerCallback = {
     //some code here...
 }

 var decisionMaker = {
     //other code here...
 }

 (1...9).forEach() { _ in
     callbacks.append(levelTimerCallback)
 }

 callbacks.append(decisionMaker)

 func timerCallback() {

     let operation = callbacks.removeFirst()
     operation()
 }

 var levelTimer = Timer.scheduledTimer(timeInterval: 0.9, target: self, selector:   #selector(timerCallback), userInfo:nil, repeats: true)

因此,这将调用levelTimer 9次,最后调用decisionMaker。虽然我还不确定,但这就是你要找的东西。