我已经构建了一个CountdownTimer,我想重构成一个单独的类,所以我可以在MainViewController中重用它。我该怎么做呢?
这是我的代码:
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()
}
}
答案 0 :(得分:2)
您可以使用闭包:
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()
}
}
}
用法,在您想要使用通用计时器的类中:
MyTimer().startTimer({ () -> Void in
timerLabel.fadeOut()
}, timerInProgress: { (elapsedTime) -> Void in
timerLabel.text = "\(Int(elapsedTime))"
})
答案 1 :(得分:0)
要在单独的类中使用countdownTimer,请创建一个新类并在MainViewController中实现该类,然后访问这些方法。
或者只是让你的功能全局化。
答案 2 :(得分:0)
XCode-> File-> New-> Source-> Cocoa Touch Class从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")
}
}
您可以随时使用您的计时器课程:
let myTimer = TimerTest()
答案 3 :(得分:0)
tbaranes答案的第4版:
class MyTimer: NSObject {
var startTime: TimeInterval! = TimeInterval()
var time: Double! = 4
var timer: Timer! = Timer()
var timerEndedCallback: (() -> Void)!
var timerInProgressCallback: ((_ elapsedTime: Double) -> Void)!
func startTimer(timerEnded: @escaping () -> Void, timerInProgress: ((_ elapsedTime: Double) -> Void)!) {
if !timer.isValid {
let aSelector : Selector = #selector(MyTimer.updateTime)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate
timerEndedCallback = timerEnded
timerInProgressCallback = timerInProgress
}
}
@objc func updateTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate
var elapsedTime = currentTime - startTime
var seconds = time - elapsedTime
if seconds > 0 {
elapsedTime -= TimeInterval(seconds)
timerInProgressCallback(elapsedTime)
} else {
timer.invalidate()
timerEndedCallback()
}
}
}