0
我在执行我上一个UITableView运行多个定时器一次,当一个定时器结束,我无效,并允许它从被删除暂停执行先前的计时器后运行的所有NSTimers说的看法。目前我遇到的问题是,当一个定时器被删除时,所有其他定时器停止执行。已经失效,而不是继续运行
public class TimerManager{
static let instance = TimerManager()
private var delegates = [TimerDelegate]()
var currentTimers = [TimerObject]()
public func timerAdded(timer: TimerObject){
self.currentTimers.append(timer)
timer.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(persistTimer(_:)), userInfo: "\(TimerManager.instance.currentTimers.count-1)", repeats: false)
timer.isRunning = true
for delegate in delegates{
delegate.startTimer(self.currentTimers[self.currentTimers.count-1])
}
}
@objc public func persistTimer(timer: NSTimer){
if let indexString = timer.userInfo as? String{
if let index = Int(indexString){
if let currentTimer = self.currentTimers[safe: index]{
if currentTimer.isRunning{
currentTimer.timeDuration -= 1
for delegate in delegates{
delegate.timerStarted(index)
}
}
}
}
}
}
public func addTime(timerId: Int, amount: Int){
for delegate in delegates{
delegate.addTimeToTimer(timerId, amount: amount)
UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
}
}
public func subtractTime(timerId: Int, amount: Int){
for delegate in delegates{
delegate.subtractTimeFromTimer(timerId, amount: amount)
UIApplication.sharedApplication().cancelLocalNotification(currentTimers[timerId].notification)
currentTimers[timerId].notification.fireDate = NSDate(timeIntervalSinceNow: Double(currentTimers[timerId].timeDuration))
}
}
public func removeDelegate(removedDelegate: TimerDelegate){
for i in 0..<delegates.count{
if delegates[i] === removedDelegate{
delegates.removeAtIndex(i)
}
}
}
public func cancelTimer(timerId: Int){
let currentTimer = currentTimers[timerId]
currentTimer.timer.invalidate()
UIApplication.sharedApplication().cancelLocalNotification(currentTimer.notification)
currentTimers.removeAtIndex(timerId)
}
private init() {}
}
这是我上面的TimerManager单例。 下面是我删除逻辑:
//Delete cell functionality
public override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete){
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let deletedTimer = TimerManager.instance.currentTimers[indexPath.row]
if (deletedTimer.isRunning){
deletedTimer.timer.invalidate()// <-- invalidates all timers for some reason
UIApplication.sharedApplication().cancelLocalNotification(deletedTimer.notification)
}
TimerManager.instance.currentTimers.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
self.tableView.frame = CGRect.init(x: 0, y: 100, width: screenWidth, height: screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
}
}
这是我如何启动一个定时器:
public func startTimer(timer: TimerObject) {
let newIndexPath = NSIndexPath(forRow: TimerManager.instance.currentTimers.count-1, inSection: 0)
self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
self.view.frame = CGRectMake(0, 100, screenWidth, screenHeight * 0.0625 * CGFloat(TimerManager.instance.currentTimers.count))
self.view.alpha = 0.95
}
这是我TimerObject所包含的内容:一TimerObject的
public class TimerObject{
var alreadySet = false
var isRunning = false
var timer = NSTimer()
var timeDuration = Int()
var timerString = String()
var doneString = String()
let notification = UILocalNotification()
init(timeDuration: Int, timerString: String, doneString: String){
self.timeDuration = timeDuration
self.timerString = timerString
self.doneString = doneString
notification.alertBody = doneString
}
}
实例化:
private func setTextLinks(string: String){
if let timersForInstruction = parsedOutTimers{
stepTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.redColor()]
for timer in timersForInstruction{
stepTextView.addLink(string.substringFromRange(timer.range)) {
let newTimer = TimerObject.init(timeDuration: timer.lowerBound * 60, timerString: self.stepTitle
newTimer.notification.fireDate = NSDate(timeIntervalSinceNow: Double(newTimer.timeDuration))
TimerManager.instance.timerAdded(newTimer)
}
}
stepTextView.processLinks()
}
}
我会说这是它使用的地方:
//where we update the cells
public func timerIsRunning(timerIndex: Int){
if let currentTimer = TimerManager.instance.currentTimers[safe: timerIndex]{
guard let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath.init(
forRow: timerIndex,
inSection: 0)) as? TimerCell else { return }
if currentTimer.timeDuration > 0{
currentTimer.timeDuration -= 1
cell.timerDisplay.text = "\(currentTimer.timerString) \t\t\t - \(currentTimer.timeDuration/60):\((currentTimer.timeDuration % 60).format("02")) + "
cell.timerDisplay.processLinks()
}else if(currentTimer.timeDuration <= 0){
currentTimer.isRunning = false
cell.timerDisplay.text = "Finished"
currentTimer.timer.invalidate()
currentTimer.notification.fireDate = NSDate(timeIntervalSinceNow: 0)
currentTimer.notification.alertBody = currentTimer.doneString
currentTimer.notification.alertAction = "Ok"
if(self.isBeingPresented()){
NSNotificationCenter.defaultCenter().postNotificationName("timerDidFinish", object: currentTimer.notification.alertBody)
}
if(UIApplicationState.Background == UIApplication.sharedApplication().applicationState){
UIApplication.sharedApplication().scheduleLocalNotification(currentTimer.notification)
}
}else{
//handle pause
}
}
}
我真诚地希望这是足够的信息来处理。任何解决方案或替代方案都会有很大帮助。
决不做'VAR定时器=的NSTimer()'。你没有展示创建和使用'TimerObject'的例子。你的问题比昨天好,但你应该真正解释你如何使用这个类,以及你的实际用户目标是什么 - 为什么你有很多计时器,为什么不有1个计时器和对象持有'火直到过期'或者只是一个NSDate时,他们到期? – Wain
对不起,我不确定如何显示计时器对象的使用情况,而不显示应用程序的屏幕截图。我会尽我所能解释。用户点击一个映射到一个动作的链接,该动作通知单例启动一个X分钟的计时器,然后启动计时器函数在显示正在运行的计时器的表格视图中创建一个新的单元格。我使用NSTimer通知persistTimer告诉tableView以新递减的时间更新单元格。用户可以启动任意数量的定时器,并且可以激活定时器任意次,这些都是约束条件。 –
使用NSDate不会让我每秒更新tableView。另外,你可以详细了解为什么'var timer = NSTimer()'是禁忌吗? –