前言

	public class NSTimer : NSObject
  • 作用

    • 在指定的时间执行指定的任务。
    • 每隔一段时间执行指定的任务。

1、定时器的创建

  • 当定时器创建完(不用 scheduled 的,添加到 runloop 中)后,该定时器将在初始化时指定的 ti 秒后自动触发。

  • scheduled 方式:

    • 创建并启动定时器。

    • 默认将时钟以 NSDefaultRunLoopMode 模式添加到运行循环。

    • 发生用户交互的时候,时钟会被暂停。

      	/*
      public class func scheduledTimerWithTimeInterval(ti: NSTimeInterval,
      target aTarget: AnyObject,
      selector aSelector: Selector,
      userInfo: AnyObject?,
      repeats yesOrNo: Bool) -> NSTimer 参数:
      TimeInterval:触发时间,单位秒
      target:定时起触发对象
      selector:定时器响应方法
      userInfo:用户信息
      repeats:是否重复执行,YES 每个指定的时间重复执行,NO 只执行一次
      */ // 创建并启动定时器
      let timer:NSTimer = NSTimer.scheduledTimerWithTimeInterval(2.0,
      target: self,
      selector: #selector(ViewController.updateTimer(_:)),
      userInfo: nil,
      repeats: true)
  • timer 方式:

    • 创建定时器,添加到运行循环后启动定时器。

    • 将时钟以指定的模式添加到运行循环。

      	/*
      + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti
      target:(id)aTarget
      selector:(SEL)aSelector
      userInfo:(nullable id)userInfo
      repeats:(BOOL)yesOrNo; - (void)addTimer:(NSTimer *)timer forMode:(NSString *)mode; mode:
      NSDefaultRunLoopMode: 时钟,网络。 发生用户交互的时候,时钟会被暂停
      NSRunLoopCommonModes: 用户交互,响应级别高。 发生用户交互的时候,时钟仍然会触发,如果时钟触发方法非常
      耗时,使用此方式时用户操作会造成非常严重的卡顿。
      */ // 创建定时器
      let timer:NSTimer = NSTimer(timeInterval: 2.0,
      target: self,
      selector: #selector(ViewController.updateTimer(_:)),
      userInfo: nil,
      repeats: true) // 将定时器添加到运行循环
      NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

2、定时器的启动与关闭

	// 启动定时器
timer.fireDate = NSDate.distantFuture() // 暂停定时器
timer.fireDate = NSDate.distantPast() // 关闭定时器,永久关闭定时器
timer.invalidate()

3、子线程定时器的创建

  • 在子线程创建定时器时,需要手动开启子线程的运行循环。

    	dispatch_async(dispatch_get_global_queue(0, 0)) { 
    
        	// 在子线程创建定时器
    /*
    scheduled 或 timer 方式创建
    */
    let timer:NSTimer = NSTimer(timeInterval: 2.0,
    target: self,
    selector: #selector(ViewController.updateTimer(_:)),
    userInfo: nil,
    repeats: true)
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) // 启动子线程的运行循环
    /*
    这句代码就是一个死循环!如果不停止运行循环,不会执行添加到此句之后的任何代码
    */
    CFRunLoopRun() // 停止子线程运行循环之前,不会执行添加到此处的任何代码
    }
    	var num:Int = 0
    
    	func updateTimer(timer:NSTimer) {
    
        	num  = num + 1
    
        	// 满足条件后,停止当前的运行循环
    if (num == 8) { // 停止当前的运行循环
    /*
    一旦停止了运行循环,后续代码能够执行,执行完毕后,线程被自动销毁
    */
    CFRunLoopStop(CFRunLoopGetCurrent())
    }
    }

4、定时任务

  • 1)performSelector

    	// 延时调用
    /*
    1.5 秒后自动调用 self 的 hideHUD 方法
    */
    self.performSelector(#selector(NsTimer.hideHUD), withObject: nil, afterDelay: 1.5) // 取消延时调用
    NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(NsTimer.hideHUD), object: nil)
  • 2)GCD

    	// 多线程
    /*
    1.5 秒后自动执行 block 里面的代码
    */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { self.hud.alpha = 0.0;
    }
  • 3)NSTimer

    	// 定时器
    /*
    1.5 秒后自动调用 self 的 hideHUD 方法
    */
    NSTimer.scheduledTimerWithTimeInterval(1.5,
    target: self,
    selector: #selector(NsTimer.hideHUD),
    userInfo: nil,
    repeats: false)

iOS - Swift NSTimer 定时器的更多相关文章

  1. iOS - OC NSTimer 定时器

    前言 @interface NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的,添加 ...

  2. IOS中的NSTimer定时器详解

    /* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...

  3. iOS中的定时器

    据我所知,iOS中的定时器有两种.一个叫NSTimer,一个叫CADisplayLink.还有一种是使用GCD,不常用,这里就不介绍了. 下边说下两个定时器分别得用法: =============== ...

  4. iOS swift的xcworkspace多项目管理(架构思想)

    iOS  swift的xcworkspace多项目管理(架构思想) 技术说明: 今天在这里分享 swift下的 xcworkspace多项目管理(架构思想),能为我们在开发中带来哪些便捷?能为我们对整 ...

  5. iOS Swift 模块练习/swift基础学习

    SWIFT项目练习     SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图  +控件 1.UIImag ...

  6. ios swift 实现饼状图进度条,swift环形进度条

    ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...

  7. Building gRPC Client iOS Swift Note Taking App

    gRPC is an universal remote procedure call framework developed by Google that has been gaining inter ...

  8. iOS Swift WisdomScanKit图片浏览器功能SDK

    iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介      WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...

  9. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

随机推荐

  1. C# ZipHelper C#公共类 压缩和解压

    关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...

  2. php socket通信(tcp/udp)

    注意 1.在socket_bind的时候ip地址不能真回环地址如127.0.0.1 2.server.php后台跑起来的时候 nohup php server.php > /var/tmp/a. ...

  3. 类型引起的bug

    1.当类型是整型时 $type = 12; 2.当类型是字符型 $type = '12';

  4. DataRow数组转换DataTable

    public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...

  5. Codeforces Canada Cup 2016

    A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. corefile的设置与使用

    一.简介 corefile是Linux下程序崩溃时生成的文件,可以用来分析程序崩溃的原因,因为它内部包含了程序崩溃时的堆栈信息. 二.corefile的设置 默认情况下,程序崩溃是不会生成corefi ...

  7. Arrays.asList()使用注意点

    今天看代码时, 发现书上使用了Arrays.asList()方法, 将一个数组转成了List, 然后说到得到的List不能调用add(), remove()方法添加元素或者删除,带着疑问看了下内部实现 ...

  8. 切分vocab时遇到的问题

    vocab的格式如下所示,每个词和对应100维的向量: </s> 0.004003 0.004419 -0.003830 -0.003278 0.001367 0.003021 0.000 ...

  9. java提高篇---HashTable

    在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相 ...

  10. 2016年10月27日 星期四 --出埃及记 Exodus 19:12

    2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...