加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中NSTimer的使用

发布时间:2020-12-14 06:19:34 所属栏目:百科 来源:网络整理
导读:github : https://github.com/potato512/SYSwiftLearning swift中使用NSTimer,效果图如下: 代码示例: // 定义常量let tagLabel = 1000; // 定义变量var timer:NSTimer!;var count:NSInteger = 0; // 实例化方法func createNSTimer(){ // init方法// self

githubhttps://github.com/potato512/SYSwiftLearning

swift中使用NSTimer,效果图如下:


代码示例:

// 定义常量
let tagLabel = 1000;
// 定义变量
var timer:NSTimer!;
var count:NSInteger = 0;
// 实例化方法
func createNSTimer()
{
        // init方法
//        self.timer = NSTimer.init(timeInterval: 0.1,target: self,selector: #selector(ViewController.countdown),userInfo: nil,repeats: true);
        // scheduled方法
        self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1,repeats: true);
        
        NSRunLoop.currentRunLoop().addTimer(self.timer,forMode: NSRunLoopCommonModes);
        self.stopNSTimer();
}
// 开始计数
func startNSTimer()
{
        if self.timer == nil
        {
            self.createNSTimer();
        }
        self.timer.fireDate = NSDate.distantPast();
}
// 停止计数
func stopNSTimer()
{
        self.timer.fireDate = NSDate.distantFuture();
}
// 注释释放
func killNSTimer()
{
        if (self.timer != nil)
        {
            if self.timer.valid
            {
                self.timer.invalidate();
                self.timer = nil;
            }
        }
        self.count = 0;
}
// 计数方法
func countdown()
{
        self.count += 1;

        let label = self.view.viewWithTag(tagLabel) as! UILabel;
        label.text = NSString(format:"%d",self.count) as String;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读