NSTimer用法总结
2016-02-16 13:19
363 查看
1、
//
// ViewController.m
// 001-nstimer
//
//
#import "ViewController.h"
@interface ViewController ()
{
NSTimer * _timer1;
NSTimer * _timer2;
NSTimer * _timer3;
NSTimer * _timer4;
NSTimer * _timer5;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
/*
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(nullable id)ui repeats:(BOOL)rep NS_DESIGNATED_INITIALIZER;
*/
// 1、
/*
以timerWithTimeInterval开头的两个方法需要手动加入到循环池中,否则不会启动
以scheduledTimerWithTimeInterval开头的两个方法则不需要手动假如循环池
五个方法都会自动执行
NSInvocation可以直接调用某个对象的消息,并可以处理多参数或者有返回值的情况,这里不再详细说明
interval设置时间间隔或者可以理解为等待多少秒后执行
repeats:YES重复执行,NO只执行一次
*/
NSInvocation * invocation1 = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(output1)]];
[invocation1 setTarget:self];
[invocation1 setSelector:@selector(output1)];
_timer1 = [NSTimer timerWithTimeInterval:3 invocation:invocation1 repeats:YES];
//加入主循环池
[[NSRunLoop mainRunLoop] addTimer:_timer1 forMode:NSDefaultRunLoopMode];
// 2、
NSInvocation * invocation2 = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(output2)]];
[invocation2 setTarget:self];
[invocation2 setSelector:@selector(output2)];
_timer2 = [NSTimer scheduledTimerWithTimeInterval:3 invocation:invocation2 repeats:YES];
// 3、
_timer3 = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(output3) userInfo:nil repeats:YES];
//加入当前循环池
[[NSRunLoop currentRunLoop] addTimer:_timer3 forMode:NSDefaultRunLoopMode];
/*
如果当前runloop没有运行,则可以调用以下方法手动运行
- (void)run;
- (void)runUntilDate:(NSDate *)limitDate;
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate;
*/
// 4、
_timer4 = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(output4) userInfo:nil repeats:YES];
// 5、
/*
这个会首先立即执行,有fireDate决定立即执行一次,然后每隔3秒执行一次,由输出也可以看出
*/
NSDate * fireDate = [NSDate date];
_timer5 = [[NSTimer alloc] initWithFireDate:fireDate interval:3 target:self selector:@selector(output5) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer5 forMode:NSDefaultRunLoopMode];
UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(10, 50, 50, 50);
btn1.backgroundColor = [UIColor orangeColor];
[btn1 setTitle:@"start1" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(start1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void) start1:(id) sender {
//首先判断定时器是否有效,如果有效,则让它无效,然后置为nil
if (_timer1.isValid) {
[_timer1 invalidate];
}
_timer1 = nil;
}
- (void) output1 {
NSLog(@"output1");
}
- (void) output2 {
NSLog(@"output2");
}
- (void) output3 {
NSLog(@"output3");
}
- (void) output4 {
NSLog(@"output4");
}
- (void) output5 {
NSLog(@"output5");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
输出:
2016-02-16 12:50:37.328 001-nstimer[1802:63641] output5
2016-02-16 12:50:40.319 001-nstimer[1802:63641] output1
2016-02-16 12:50:40.320 001-nstimer[1802:63641] output2
2016-02-16 12:50:40.320 001-nstimer[1802:63641] output3
2016-02-16 12:50:40.321 001-nstimer[1802:63641] output4
2016-02-16 12:50:40.321 001-nstimer[1802:63641] output5
2016-02-16 12:50:43.320 001-nstimer[1802:63641] output1
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output2
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output3
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output4
2016-02-16 12:50:43.322 001-nstimer[1802:63641] output5
2、成员变量
设置定时器的启动时间
@property (copy) NSDate *fireDate;
只读属性,获取定时器的调用时间间隔
@property (readonly) NSTimeInterval timeInterval;
7.0之后新增的数学,设置误差范围
@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);
判断定时器是否有效
@property (readonly, getter=isValid) BOOL valid;
获取参数信息
@property (nullable, readonly, retain) id userInfo;
//
// ViewController.m
// 001-nstimer
//
//
#import "ViewController.h"
@interface ViewController ()
{
NSTimer * _timer1;
NSTimer * _timer2;
NSTimer * _timer3;
NSTimer * _timer4;
NSTimer * _timer5;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
/*
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(nullable id)ui repeats:(BOOL)rep NS_DESIGNATED_INITIALIZER;
*/
// 1、
/*
以timerWithTimeInterval开头的两个方法需要手动加入到循环池中,否则不会启动
以scheduledTimerWithTimeInterval开头的两个方法则不需要手动假如循环池
五个方法都会自动执行
NSInvocation可以直接调用某个对象的消息,并可以处理多参数或者有返回值的情况,这里不再详细说明
interval设置时间间隔或者可以理解为等待多少秒后执行
repeats:YES重复执行,NO只执行一次
*/
NSInvocation * invocation1 = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(output1)]];
[invocation1 setTarget:self];
[invocation1 setSelector:@selector(output1)];
_timer1 = [NSTimer timerWithTimeInterval:3 invocation:invocation1 repeats:YES];
//加入主循环池
[[NSRunLoop mainRunLoop] addTimer:_timer1 forMode:NSDefaultRunLoopMode];
// 2、
NSInvocation * invocation2 = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(output2)]];
[invocation2 setTarget:self];
[invocation2 setSelector:@selector(output2)];
_timer2 = [NSTimer scheduledTimerWithTimeInterval:3 invocation:invocation2 repeats:YES];
// 3、
_timer3 = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(output3) userInfo:nil repeats:YES];
//加入当前循环池
[[NSRunLoop currentRunLoop] addTimer:_timer3 forMode:NSDefaultRunLoopMode];
/*
如果当前runloop没有运行,则可以调用以下方法手动运行
- (void)run;
- (void)runUntilDate:(NSDate *)limitDate;
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate;
*/
// 4、
_timer4 = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(output4) userInfo:nil repeats:YES];
// 5、
/*
这个会首先立即执行,有fireDate决定立即执行一次,然后每隔3秒执行一次,由输出也可以看出
*/
NSDate * fireDate = [NSDate date];
_timer5 = [[NSTimer alloc] initWithFireDate:fireDate interval:3 target:self selector:@selector(output5) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer5 forMode:NSDefaultRunLoopMode];
UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(10, 50, 50, 50);
btn1.backgroundColor = [UIColor orangeColor];
[btn1 setTitle:@"start1" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(start1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void) start1:(id) sender {
//首先判断定时器是否有效,如果有效,则让它无效,然后置为nil
if (_timer1.isValid) {
[_timer1 invalidate];
}
_timer1 = nil;
}
- (void) output1 {
NSLog(@"output1");
}
- (void) output2 {
NSLog(@"output2");
}
- (void) output3 {
NSLog(@"output3");
}
- (void) output4 {
NSLog(@"output4");
}
- (void) output5 {
NSLog(@"output5");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
输出:
2016-02-16 12:50:37.328 001-nstimer[1802:63641] output5
2016-02-16 12:50:40.319 001-nstimer[1802:63641] output1
2016-02-16 12:50:40.320 001-nstimer[1802:63641] output2
2016-02-16 12:50:40.320 001-nstimer[1802:63641] output3
2016-02-16 12:50:40.321 001-nstimer[1802:63641] output4
2016-02-16 12:50:40.321 001-nstimer[1802:63641] output5
2016-02-16 12:50:43.320 001-nstimer[1802:63641] output1
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output2
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output3
2016-02-16 12:50:43.321 001-nstimer[1802:63641] output4
2016-02-16 12:50:43.322 001-nstimer[1802:63641] output5
2、成员变量
设置定时器的启动时间
@property (copy) NSDate *fireDate;
只读属性,获取定时器的调用时间间隔
@property (readonly) NSTimeInterval timeInterval;
7.0之后新增的数学,设置误差范围
@property NSTimeInterval tolerance NS_AVAILABLE(10_9, 7_0);
判断定时器是否有效
@property (readonly, getter=isValid) BOOL valid;
获取参数信息
@property (nullable, readonly, retain) id userInfo;
相关文章推荐
- Mootools 1.2教程 定时器和哈希简介
- C#定时器和随机数
- C#中timer定时器用法实例
- JS中自定义定时器让它在某一时刻执行
- JavaScript定时器详解及实例
- 基于jQuery实现的扇形定时器附源码下载
- 跟我学习javascript的定时器
- Qt定时器和随机数详解
- asp.net中Timer无刷新定时器的实现方法
- libevent库的使用--定时器的使用实例
- MySQL定时器EVENT学习笔记
- js函数定时器实现定时读取系统实时连接数
- linux使用select实现精确定时器详解
- Cocos2d-x Schedule定时器的使用实例
- VC定时器的用法实例详解
- javascript设计简单的秒表计时器
- c#定时器和global实现自动job示例
- JavaScript学习笔记之定时器
- js定时器的使用(实例讲解)
- c#定时器使用示例详解