当前位置:网站首页>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]//立即停止
边栏推荐
猜你喜欢
随机推荐
Oracle数据库的两种进入方式
备战金三银四:如何成功拿到阿里offer(经历+面试题+如何准备)
golang源代码阅读,sync系列-Pool
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization Paper Reading
golang 三种指针类型具体类型的指针、unsafe.Pointer、uintptr作用
GOPROXY 中国代理
PTA 指定位置输出字符串(c)
jmeter BeanShell 后置处理器
Tensorflow realize parameter adjustment of linear equations
从位图到布隆过滤器
pip常见命令和更改源文件
最长回文子串
torch.stack()的官方解释,详解以及例子
【VIBE: Video Inference for Human Body Pose and Shape Estimation】论文阅读
tensorflow和numpy对应的版本,报FutureWarning: Passing (type, 1) or ‘1type‘ as a synonym of type is deprecate
Input and output of cnn
OpenSSF's open source software risk assessment tool: Scorecards
sublime记录
golang源代码阅读,sync系列-Cond
无重复字符的最长子串