ios – 计时器结束后如何自动切换?

ios – 计时器结束后如何自动切换?,第1张

概述我想在计时器用完时自动切换. 我在这里构建了一个计时器: class Timer{var timer = NSTimer();// the callback to be invoked everytime the timer 'ticks'var handler: (Int) -> ();//the total duration in seconds for which the timer 我想在计时器用完时自动切换.

我在这里构建了一个计时器:

class Timer{var timer = NSTimer();// the callback to be invoked everytime the timer 'ticks'var handler: (Int) -> ();//the total duration in seconds for which the timer should run to be set by the callerlet duration: Int;//the amount of time in seconds elapsed so farvar elapsedtime: Int = 0;var targetController = WaitingRoomController.self/**:param: an integer duration specifying the total time in seconds for which the timer should run repeatedly:param: handler is reference to a function that takes an Integer argument representing the elapsed time allowing the implementor to process elapsed time and returns voID*/init(duration: Int,handler : (Int) -> ()){    self.duration = duration;    self.handler = handler;}/**Schedule the Timer to run every 1 second and invoke a callback method specifIEd by 'selector' in repeating mode*/func start(){    self.timer = NSTimer.scheduledTimerWithTimeInterval(1,target: self,selector: "onTick",userInfo: nil,repeats: true);}/**invalIDate the timer*/func stop(){    println("timer was invaIDated from stop()")    timer.invalIDate();}/**Called everytime the timer 'ticks'. Keep track of the total time elapsed and trigger the handler to notify the implementors of the current 'tick'. If the amount of time elapsed is the same as the total duration for the timer was intended to run,stop the timer.*/@objc func onTick() {    //println("onTick")    //increment the elapsed time by 1 second    self.elapsedtime++;    //Notify the implementors of the updated value of elapsed time    self.handler(elapsedtime);    //If the amount of elapsed time in seconds is same as the total time in seconds for which this timer was intended to run,stop the timer    if self.elapsedtime == self.duration {        self.stop();    }}deinit{    println("timer was invalIDated from deinit()")    self.timer.invalIDate();}

}

这是我想要执行segue以及计时器如何启动的视图控制器:

class WaitingRoomController: UIVIEwController {    private func handleSetAction(someTime: String){       countDownLabel.text = setTime;       let duration = Utils.getTotalDurationInSeconds(someTime);       timer = Timer(duration: duration ){          (elapsedtime: Int) -> () in             println("handler called")            let difference = duration - elapsedtime;            self.countDownLabel.text = Utils.getDurationInMinutesAndSeconds(difference)       }       timer.start();    }}

我知道auto segue的代码是:

func doSegue(){    self.performSegueWithIDentifIEr("asdf",sender: self)}

但我不知道如何将计时器和这个功能连接在一起.

解决方法 你需要使用闭包来实现你想要的,看看我的班级Timer.

import UIKitclass Timer: NSObject {    var counter: Int = 0    var timer: NSTimer! = NSTimer()    var timerEndedCallback: (() -> VoID)!    var timerInProgressCallback: ((elapsedtime: Int) -> VoID)!    func startTimer(duration: Int,timerEnded: () -> VoID,timerInProgress: ((elapsedtime: Int) -> VoID)!) {       if !(self.timer?.valID != nil) {           let aSelector : Selector = "updateTime:"           timer = NSTimer.scheduledTimerWithTimeInterval(1,selector: aSelector,userInfo: duration,repeats: true)           timerEndedCallback = timerEnded           timerInProgressCallback = timerInProgress           counter = 0       }    }    func updateTime(timer: NSTimer) {       ++self.counter       let duration = timer.userInfo as! Int       if (self.counter != duration) {          timerInProgressCallback(elapsedtime: self.counter)       } else {          timer.invalIDate()          timerEndedCallback()       }    }}

然后您可以通过以下方式调用Timer类:

var timer = Timer()timer.startTimer(5,timerEnded: { () -> VoID in        // Here you call anything you want when the timer finish.        println("Finished")        },timerInProgress: { (elapsedtime) -> VoID in            println("\(Int(elapsedtime))")})

上面的类在它已经有效时处理定时器的使用,并注意到duration参数使用timer参数中的userInfo传递给updateTime处理程序.

我希望这对你有帮助.

总结

以上是内存溢出为你收集整理的ios – 计时器结束后如何自动切换?全部内容,希望文章能够帮你解决ios – 计时器结束后如何自动切换?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存