下面罗列几种常用的创建Timer的方式
1. + (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void(^)(NSTimer*timer))block
2. + (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullableid)userInfo repeats:(BOOL)yesOrNo
3. + (NSMethodSignature*)instanceMethodSignatureForSelector:(SEL)aSelectorOBJC_SWIFT_UNAVAILABLE("")
4. + (NSTimer*)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void(^)(NSTimer*timer))block
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createTimerAndRun];
[self createTimerAndRun2];
[self createTimerAndRun3];
[self createTimerAndRun4];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)createTimerAndRun {
staticintcount = 0;
//interval 参数单位是秒,这里的意思是没3秒执行一次。
__block NSTimer *scheduleTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
count ++;
NSLog(@"-- current the timer count is %d", count);
if(count == 10) {
//终止timer的运行
[scheduleTimerinvalidate];
scheduleTimer =NULL;
}
}];
}
- (void)createTimerAndRun2 {
//可以在创建timer的同时设置userinfo
self.userinfoTimer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(triggerTimer) userInfo:[NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil] repeats:NO];
}
- (void)triggerTimer {
NSArray *userInfo = self.userinfoTimer.userInfo;
for(idvalueinuserInfo) {
NSLog(@"-- the timer user info is %@", value);
}
}
- (void)createTimerAndRun3 {
//可以创建NSInvocation实例
NSDate*date = [NSDatedate];
NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys:@"lisa test", @"key", nil];
SELmethod =@selector(fireTimer:andDate:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:method];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocationsetTarget:self];
[invocationsetSelector:method];
//给selector的参数设置值
[invocationsetArgument:&useratIndex:2];
[invocationsetArgument:&dateatIndex:3];
//启动invocation
[invocationinvoke];
}
- (void)fireTimer:(NSDictionary*)user andDate:(NSDate*)startTime {
/*
sleep 与 sleepForTimeInterval的区别
sleep直接让线程停掉,sleepForTimeInterval是让runLoop停掉。比如说,你有2个APP,分别是A和B,A启动B,然后去取B的进程号,如果你用sleep等B启动再去取,你会发现取不到,因为你只是把代码加到runloop里面去,而runloop并没有执行到这句,sleep就直接让系统停在那里,所以取不到,而后者就没问题,因为它是让runloop执行到这句的时候停1s
*/
[NSThread sleepForTimeInterval:2];
NSTimeIntervaltimeInterval = -1 * [startTimetimeIntervalSinceNow];
NSString*timeStr = [NSStringstringWithFormat:@"%.2f", timeInterval];
NSString*userInfo = [uservalueForKey:@"key"];
NSLog(@"fireTime: %@ and userinfo is %@", timeStr, userInfo);
}
- (void)createTimerAndRun4 {
staticintcount = 0;
self.intervalTimer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
count ++;
NSLog(@"--- the count is %d", count);
if(count == 7) {
[self.intervalTimer invalidate];
self.intervalTimer=nil;
}
}];
//添加到runloop中,不同于scheduledTimer, 这种方式创建的timer是没有假如到runloop中
[[NSRunLoop currentRunLoop]addTimer:self.intervalTimer forMode:NSDefaultRunLoopMode];
[self.intervalTimer fire];
}