如何在一个单独的类中使用iOS Swift设置一个计时器?

时间:2021-08-02 02:45:06

I've built a CountdownTimer which i would like to refactor into a separate class so i can reuse it in the MainViewController. How would i go about doing that?

我已经构建了一个CountdownTimer我想把它重构到一个单独的类中这样我就可以在MainViewController中重用它。我该怎么做呢?

This is my code:

这是我的代码:

var startTime = NSTimeInterval()
var time:Double = 4
var timer = NSTimer()

/* Outlets */
@IBOutlet weak var timerLabel: UILabel!

 /* CountdownTimer function */
func updateTime() {

    var currentTime = NSDate.timeIntervalSinceReferenceDate()
    var elapsedTime = currentTime - startTime
    var seconds = time - elapsedTime

    if seconds > 0 {
        elapsedTime -= NSTimeInterval(seconds)
        timerLabel.text = "\(Int(seconds))"
    } else {
        timer.invalidate()
        timerLabel.fadeOut()
    }
}

func startTimer () {
    if !timer.valid {
        let aSelector : Selector = "updateTime"
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
        startTime = NSDate.timeIntervalSinceReferenceDate()
    }
}

3 个解决方案

#1


2  

You can use a closure:

你可以使用一个闭包:

class MyTimer: NSObject {

    var startTime: NSTimeInterval! = NSTimeInterval()
    var time: Double! = 4
    var timer: NSTimer! = NSTimer()
    var timerEndedCallback: (() -> Void)!
    var timerInProgressCallback: ((elapsedTime: Double) -> Void)!

    func startTimer(timerEnded: () -> Void, timerInProgress: ((elapsedTime: Double) -> Void)!) {
        if !timer.valid {
            let aSelector : Selector = "updateTime"
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
            startTime = NSDate.timeIntervalSinceReferenceDate()
            timerEndedCallback = timerEnded
            timerInProgressCallback = timerInProgress
        }
    }

     func updateTime() {

        var currentTime = NSDate.timeIntervalSinceReferenceDate()
        var elapsedTime = currentTime - startTime
        var seconds = time - elapsedTime

        if seconds > 0 {
            elapsedTime -= NSTimeInterval(seconds)
            timerInProgressCallback(elapsedTime: elapsedTime)
        } else {
            timer.invalidate()
            timerEndedCallback()
        }
    }
}

Usage, in the class you want to use your generic timer:

在类中,您希望使用通用计时器:

MyTimer().startTimer({ () -> Void in
    timerLabel.fadeOut()
}, timerInProgress: { (elapsedTime) -> Void in
    timerLabel.text = "\(Int(elapsedTime))"
})

#2


0  

To use your countdownTimer in a separate class, create a new class and implement the class in your MainViewController and then access the methods.

要在单独的类中使用countdownTimer,请创建一个新类,并在MainViewController中实现类,然后访问方法。

Alternatively just make your functions global.

或者只是使你的函数全局化。

#3


0  

XCode->File->New->Source->Cocoa Touch Class extends from NSObject

XCode->文件->New->源代码->Cocoa Touch类扩展自NSObject

import UIKit
class TimerTest: NSObject {
    var myTimer:NSTimer?
    override init() {
        super.init()
        print("init worked")
        myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerFuncTriggered:", userInfo: nil, repeats: true)
    }
    func timerFuncTriggered(timer:NSTimer) {
        print("timer started")
    }
}

you can use your timer class anytime like:

您可以随时使用您的计时器类,如:

let myTimer = TimerTest()

#1


2  

You can use a closure:

你可以使用一个闭包:

class MyTimer: NSObject {

    var startTime: NSTimeInterval! = NSTimeInterval()
    var time: Double! = 4
    var timer: NSTimer! = NSTimer()
    var timerEndedCallback: (() -> Void)!
    var timerInProgressCallback: ((elapsedTime: Double) -> Void)!

    func startTimer(timerEnded: () -> Void, timerInProgress: ((elapsedTime: Double) -> Void)!) {
        if !timer.valid {
            let aSelector : Selector = "updateTime"
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
            startTime = NSDate.timeIntervalSinceReferenceDate()
            timerEndedCallback = timerEnded
            timerInProgressCallback = timerInProgress
        }
    }

     func updateTime() {

        var currentTime = NSDate.timeIntervalSinceReferenceDate()
        var elapsedTime = currentTime - startTime
        var seconds = time - elapsedTime

        if seconds > 0 {
            elapsedTime -= NSTimeInterval(seconds)
            timerInProgressCallback(elapsedTime: elapsedTime)
        } else {
            timer.invalidate()
            timerEndedCallback()
        }
    }
}

Usage, in the class you want to use your generic timer:

在类中,您希望使用通用计时器:

MyTimer().startTimer({ () -> Void in
    timerLabel.fadeOut()
}, timerInProgress: { (elapsedTime) -> Void in
    timerLabel.text = "\(Int(elapsedTime))"
})

#2


0  

To use your countdownTimer in a separate class, create a new class and implement the class in your MainViewController and then access the methods.

要在单独的类中使用countdownTimer,请创建一个新类,并在MainViewController中实现类,然后访问方法。

Alternatively just make your functions global.

或者只是使你的函数全局化。

#3


0  

XCode->File->New->Source->Cocoa Touch Class extends from NSObject

XCode->文件->New->源代码->Cocoa Touch类扩展自NSObject

import UIKit
class TimerTest: NSObject {
    var myTimer:NSTimer?
    override init() {
        super.init()
        print("init worked")
        myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerFuncTriggered:", userInfo: nil, repeats: true)
    }
    func timerFuncTriggered(timer:NSTimer) {
        print("timer started")
    }
}

you can use your timer class anytime like:

您可以随时使用您的计时器类,如:

let myTimer = TimerTest()