简体   繁体   English

如何将这个Swift 3 Singleton用作我的IOS项目的全局计时器?

[英]How can I use this Swift 3 Singleton as a Global Timer for my IOS project?

I've found this code on another thread (shown at the very bottom) for Swift 3 and can't seem to get it to work in my IOS project. 我在Swift 3的另一个线程(显示在最底层)找到了这个代码,似乎无法让它在我的IOS项目中工作。

I know it's a Singleton, but I'll have to admit, I'm not sure how to USE it in my IOS project so that the timer will work across all ViewControllers. 我知道这是一个Singleton,但我不得不承认,我不知道如何在我的IOS项目中使用它,以便计时器可以在所有ViewControllers中使用。

Can I do the following? 我可以这样做吗? And if not, how can I use this? 如果没有,我该如何使用它?

var t = TimeModel.sharedTimer

t.startTimer(0.25, testing)

var counter = 0

func testing()
{
    counter += 1
    print("this is a test \(counter)")
}

What am I doing wrong? 我究竟做错了什么?

class TimerModel: NSObject {
    static let sharedTimer: TimerModel = {
        let timer = TimerModel()
        return timer
    }()

    var internalTimer: Timer?
    var jobs = [() -> Void]()

    func startTimer(withInterval interval: Double, andJob job: @escaping () -> Void) {
        if internalTimer == nil {
            internalTimer?.invalidate()
        }
        jobs.append(job)
        internalTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(doJob), userInfo: nil, repeats: true)
    }

    func pauseTimer() {
        guard internalTimer != nil else {
            print("No timer active, start the timer before you stop it.")
            return
        }
        internalTimer?.invalidate()
    }

    func stopTimer() {
        guard internalTimer != nil else {
            print("No timer active, start the timer before you stop it.")
            return
        }
        jobs = [()->()]()
        internalTimer?.invalidate()
    }

    func doJob() {
        for job in jobs {
            job()
        }
    }

}

// Call the method like this. //像这样调用方法

let timer = TimerModel.sharedTimer

timer.startTimer(withInterval: 0.25) { 
    self.testing()
}

var counter = 0
func testing() {
    counter += 1
    print("this is a test \(counter)")
}

在此输入图像描述

If you want to call timer's selector method in another ViewController 如果要在另一个ViewController中调用timer的selector方法

You need to make some changes in your method ; 您需要对方法进行一些更改;

func startTimer(withInterval interval: Double, controller: UIViewController, andJob job: @escaping () -> Void) {
                    if timerTest != nil {
                    internalTimer.invalidate()
                    internalTimer = nil
                 }
                    jobs.append(job)
                    internalTimer = Timer.scheduledTimer(timeInterval: interval, target: controller, selector: #selector(doJob), userInfo: nil, repeats: true)
                }

Call in another ViewController 在另一个ViewController中调用

var t = TimeModel.sharedTimer.startTimer(0.25, self , testing)
    var counter = 0
     func doJob() {
        counter += 1
        print("this is a test \(counter)")
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM