iOS 定时器的使用以及注意事项
来源:互联网 发布:2017最新网络灰色项目 编辑:程序博客网 时间:2024/05/22 14:41
我在工程中写了一个方法
- (void)timecount{
NSDate* date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm:ss"];
NSString *time = [formatter stringFromDate:date];
NSLog(@"%@",time);
}
用于打印时间字符串的
首先定义一个变量
NSTimer *_timer;
然后实现计时器功能
利用NSTimer
方法1、创建并绑定计时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timecount) userInfo:nil repeats:YES];
效果如下:
方法2、
_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timecount) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
注意,上面的runloop是在主线程中开启的,要在子线程开启,就需要手动开启了
方法3、子线程中开启runloop
[NSThread detachNewThreadSelector:@selector(useNSTimer) toTarget:self withObject:nil];
实现方法useNSTimer
- (void)useNSTimer{
_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timecount) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
// runloop在子线程上是需要手动开启的
[[NSRunLoop currentRunLoop] run];
}
如果要销毁子线程中的定时器,就要在子线程中调用销毁方法,否则会造成runloop资源的浪费
- (void)useNSTimer{
_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timecount) userInfo:nil repeats:YES];
[self performSelector:@selector(stopcount) withObject:nil afterDelay:10];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
//// runloop在子线程上是需要手动开启的
[[NSRunLoop currentRunLoop] run];
}
上面的代码就是在10秒后销毁定时器
- (void)stopcount{
[_timer invalidate];
_timer = nil;
}
效果图如下:
注意,runloop对timer有了强引用,weak型的timer不会被释放,所以要在适当的时候销毁timer对象
上面的timer都会造成循环引用,我们可以给NSTimer扩展一个方法:
定义并实现
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval block:(void (^)())block repeats:(BOOL)repeats{
return [NSTimer timerWithTimeInterval:interval target:self selector:@selector(timerSelector:) userInfo:block repeats:repeats];
}
+ (void)timerSelector:(NSTimer *)timer{
void (^ block)() =[timer userInfo];
if (block) {
block();
}
}
在调用时倒入我们写的扩展
__weak typeof(self)weakSelf = self;
_timer = [NSTimer timerWithTimeInterval:1 block:^{
[weakSelf timecount];
} repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
如此就避免了timer带来的循环引用问题
在iOS10以后,苹果提供了带block的方法就避免了循环引用
_timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
}];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
或者
_timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
}];
利用GCD实现计时器
- (void)usegcd:(NSTimeInterval)timeinterval repeats:(BOOL)repeats{
__weak typeof(self)weakSelf = self;
dispatch_queue_t queue = dispatch_queue_create("计时器", 0);
dispatch_source_t time = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(time, DISPATCH_TIME_NOW, NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(time, ^{
if (!repeats) {
dispatch_cancel(time);
}
else
[weakSelf timecount];
});
dispatch_resume(time);
}
- iOS 定时器的使用以及注意事项
- IOS定时器的使用
- iOS-定时器的使用
- IOS学习笔记之十四:NotificationCenter的使用以及注意事项
- iOS基础知识:if 语句以及if语句的使用注意事项
- (iOS开发)UIScrollView的使用以及注意事项
- AlarmManager的定时器方法使用注意事项
- CADisplayLink以及定时器的使用
- 注解的使用以及注意事项
- SimpleDateFormat的使用以及注意事项
- PopupWindow的使用以及注意事项
- strcpy的使用以及注意事项
- IOS 定时器NSTimer的使用
- iOS NSTimer 定时器的使用
- iOS中定时器的使用
- iOS开发,定时器的使用
- iOS-定时器的使用NSTimer
- iOS开发,定时器的使用
- 云付注册推荐人怎么填?云付新手怎么操作
- AJAX请求数据步骤是什么?传输的数据是用的暗文还是明文?
- Java并发之BlockingQueue
- js正则表达式校验正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
- IPv4寿命延长:NAT地址转换
- iOS 定时器的使用以及注意事项
- Harris Corner Detection&image Matching
- CSS--定位
- Hough Translation & Line Detection & Circle Detection
- 第一章 理解网络编程和套接字
- 大学必须掌握的软件基础课程
- 获取文件属性的文件系统相关函数
- 萌新的linux之旅12
- 大学里应该掌握的两个能力