代码:
//定义全局变量
NSTimer * _timer;//计时器
#pragma mark - timer相关
/**添加计时器*/
-(void)addTimer {
if (!_timer) {
__weak __typeof__(self)weakSelf=self;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:true block:^(NSTimer * _Nonnull timer) {
//...需要做什么
}];
}
}
/**移除计时器*/
-(void)removeTimer {
[_timer invalidate];
_timer = nil;
}