您的位置:首页 > 其它

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nstimer 定时器