[iOS]NSTimer 不触发事件的解决
2016-03-16 10:13
337 查看
1.创建NSTimer
使用scheduledTimerWithTimeInterval方法创建的NSTimer会以默认方式加入当前NSRunLoop中使用 timerWithTimeInterval initWithFireDate 创建需要手动加入一个NSRunLoop中
scheduledTimerWithTimeInterval:invocation:repeats: scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: timerWithTimeInterval:invocation:repeats: timerWithTimeInterval:target:selector:userInfo:repeats: initWithFireDate:interval:target:selector:userInfo:repeats:
2 使用NSTimer
//创建计时器,计时器创建后会自动开始计时 let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: Selector("onTimer:"), userInfo: nil, repeats: true) timer.fire() //触发onTimer事件,本次触发不影响计时器计时 timer.invalidate() //停止计时器
在其他线程创建计时器时,不触发onTimer,需要在主线程中创建
dispatch_sync(dispatch_get_main_queue(), { () -> Void in NSTimer.scheduledTimerWithTimeInterval(timeInterval, target: self, selector: Selector("onTimer:"), userInfo: nil, repeats: true) })
相关文章推荐
- IOS开发实现录音功能
- iOS SDK中使用NSXMLParser解析XML
- IOS 改变按钮标题位置和缩进
- iOS开发画虚线的方法
- iOS开发画虚线的方法
- iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点
- IOS 改变提示placeholder颜色和大小
- iOS开发实现音频播放功能
- 适配iOS9出现的一些问题
- iOS自定义导航栏形状
- 点击任意空白收回键盘
- 文本框随键盘动态改变位置(获取键盘弹起高度)
- iOS开发 蓝牙4.0的各种亲身经历踩过得坑
- iOS开发实战之真机调试(免证书)
- iOS项目目录结构
- IOS taBbar item角标设置
- 【转】在写一个iOS应用之前必须做的7件事(附相关资源)
- ios开发笔记之TextView
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决的方法
- IOS之开发者申请测试证书步骤