当前位置:网站首页>OC-NSTimer
OC-NSTimer
2022-08-09 11:08:00 【彭同学她同桌】
必须运行在NSRunLoop上
@interface A:NSObject
-(void)openTimer;
-(void)doAction:(NSTimer*)timer;
@end
@implementation A
-(void)openTimer
{
NSDictionary *info = @{
@"name":@"dahuang"};
//创建计时器,自动将计时器放入runloop
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(doAction:) userInfo:nil repeats:YES];
//TimeInterVal是时间也就是每多少秒执行一次 target是执行的函数在哪 selector是要执行的方法是什么 userInfo执行函数之后会打印相应信息 repeats是是否重复执行
//手动开启runloop
[[NSRunLoop mainRunLoop]run];//手动开启runloop
//创建计时器 但是不会自动放进计时器
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(doAction:) userInfo:nil repeats:YES];
//手动将计时器放进runloop
[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSEunLoop mainRunLoop]run];
}
-(void)doAction:(NSTimer*)timer
{
NSLog(@"%s",__func__);
}
@end
int main()
{
A *a = [[A alloc]init];
[a openTimer];//
return 0;
}
fire
[timer fire]//会立即执行
invalidate
[timer invalidate]//立即停止
边栏推荐
猜你喜欢
信息系统项目的十大管理
双向链表的各种操作
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
tensorflow实现线性方程的参数调整
无刷无霍尔BLCD电机控制
Netscope: Online visualization tool for neural network structures
性能测试(05)-表达式和业务关联-json关联
MDK添加注释模板
Preparation for gold three silver four: how to successfully get an Ali offer (experience + interview questions + how to prepare)
b站up主:空狐公子 --矩阵求导(分母布局)课程笔记
随机推荐
支付宝小程序的接入
C语言统计不同单词数
String类型的字符串对象转实体类和String类型的Array转List
activemq 消息持久化
Quartz的理解
MDK添加注释模板
caffe ---make all editing error
解决1.tensorflow运行使用CPU不使用GPU 2.tensorflow环境下的GPU版本号 3.tensorflow和cuda以及cudnn版本对应问题 4.查看cuda和cudnn版本
MATLAB代码实现三次样条插值
获取指定年度所有周的工具类
PTA习题 阶梯电价(C)
Missing URI template variable ‘employeeNumber‘ for method parameter of type String
electron 应用开发优秀实践
fork创建多个子进程
cnn的输入输出
全网最简单解决OneNote中英字体不统一
prometheus接入mysqld_exporter
Oracle数据库的两种进入方式
golang runtime Caller、Callers、CallersFrames、FuncForPC、Stack作用
备份mongodb数据库(认证)