swift代码之路(五)NSTimer

来源:互联网 发布:浙江广信数据有限公司 编辑:程序博客网 时间:2024/05/21 17:23
[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public class NSTimer : NSObject  

定时器的作用:

1、在指定的时间执行指定的任务
2、间隔一段时间执行指定任务

定时器的创建

定时器有两种创建方式
(1)scheduled方式
  • 创建并启动定时器
  • 默认将时钟以NSDefaultRunLoopMode模式添加到运行循环
  • 用户发生交互的时候时钟将暂停
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.        public class func scheduledTimerWithTimeInterval(ti: NSTimeInterval,  
  3.                                             target aTarget: AnyObject,  
  4.                                         selector aSelector: Selector,  
  5.                                                   userInfo: AnyObject?,  
  6.                                            repeats yesOrNo: Bool) -> NSTimer 
  7.  
  8.        参数: 
  9.            TimeInterval:触发时间,单位秒 
  10.            target:定时起触发对象 
  11.            selector:定时器响应方法 
  12.            userInfo:用户信息 
  13.            repeats:是否重复执行,YES 每个指定的时间重复执行,NO 只执行一次 
  14.    */  
  15.   
  16.    // 创建并启动定时器  
  17.    let timer:NSTimer = NSTimer.scheduledTimerWithTimeInterval(2.0,   
  18.                                                        targetself,   
  19.                                                      selector: #selector(YPScrollView.timerChange),2  
  20.                                                      userInfo: nil,   
  21.                                                       repeatstrue)  

(2)timer方式
  • 创建定时器添加到运行循环
  • 将时钟以指定模式添加到运行循环
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.     mode: 
  3.         NSDefaultRunLoopMode: 时钟,网络。           发生用户交互的时候,时钟会被暂停 
  4.         NSRunLoopCommonModes: 用户交互,响应级别高。  发生用户交互的时候,时钟仍然会触发,如果时钟触发方法非常 
  5.                                                     耗时,使用此方式时用户操作会造成非常严重的卡顿。 
  6. */  
  7.   
  8. // 创建定时器  
  9. let timer:NSTimer = NSTimer(timeInterval: 2.0,   
  10.                                   targetself,   
  11.                                 selector: #selector(ViewController.updateTimer(_:)),   
  12.                                 userInfo: nil,   
  13.                                  repeatstrue)  
  14.   
  15. // 将定时器添加到运行循环  
  16. NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)  

定时器的启动与关闭

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. // 启动定时器  
  2. timer.fireDate = NSDate.distantFuture()  
  3.       
  4. // 暂停定时器  
  5. timer.fireDate = NSDate.distantPast()  
  6.       
  7. // 关闭定时器,永久关闭定时器  
  8. timer.invalidate()  

子线程定时器的创建

  • 在子线程创建定时器时,需要手动开启子线程的运行循环
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. dispatch_async(dispatch_get_global_queue(00)) {   
  2.   
  3.       // 在子线程创建定时器  
  4.       /* 
  5.           scheduled 或 timer 方式创建 
  6.       */  
  7.       let timer:NSTimer = NSTimer(timeInterval: 2.0,   
  8.                            targetself,   
  9.                          selector: #selector(ViewController.updateTimer(_:)),   
  10.                          userInfo: nil,   
  11.                           repeatstrue)  
  12.       NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)  
  13.   
  14.       // 启动子线程的运行循环  
  15.       /* 
  16.           这句代码就是一个死循环!如果不停止运行循环,不会执行添加到此句之后的任何代码 
  17.       */  
  18.       CFRunLoopRun()  
  19.   
  20.       // 停止子线程运行循环之前,不会执行添加到此处的任何代码  
  21.   }  

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. var num:Int = 0  
  2.   
  3.   func updateTimer(timer:NSTimer) {  
  4.   
  5.       num  = num + 1  
  6.   
  7.       // 满足条件后,停止当前的运行循环  
  8.       if (num == 8) {  
  9.   
  10.           // 停止当前的运行循环  
  11.           /* 
  12.               一旦停止了运行循环,后续代码能够执行,执行完毕后,线程被自动销毁 
  13.           */  
  14.           CFRunLoopStop(CFRunLoopGetCurrent())  
  15.       }  
  16.   }  

定时任务

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. // 延时调用  
  2. /* 
  3.     1.5 秒后自动调用 self 的 hideHUD 方法 
  4. */  
  5. self.performSelector(#selector(NsTimer.hideHUD), withObject: nil, afterDelay1.5)  
  6.   
  7. // 取消延时调用  
  8. NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(NsTimer.hideHUD), object: nil)  

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. // 多线程  
  2.   /* 
  3.       1.5 秒后自动执行 block 里面的代码 
  4.   */  
  5.   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {  
  6.   
  7.       self.hud.alpha = 0.0;  
  8.   }  

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. // 定时器  
  2. /* 
  3.     1.5 秒后自动调用 self 的 hideHUD 方法 
  4. */  
  5. NSTimer.scheduledTimerWithTimeInterval(1.5,   
  6.                                 targetself,   
  7.                               selector: #selector(NsTimer.hideHUD),   
  8.                               userInfo: nil,   
  9.                                repeatsfalse
0 0
原创粉丝点击