
场景:当主线程默认的runloop模式是 NSDefaultRunLoopMode模式,而textview控件拖拽时是在UITrackingRunLoopMode模式下工作的,这样造成在拖拽时NSTimer停止工作
场景1
创建方式1
// 系统默认帮你将timer加入runloop
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
创建方式2
//2.添加到runloop中,指定运行模式为NSDefaultRunLoopMode(默认)
//只有当runloop处于NSDefaultRunLoopMode运行模式下的时候定时器才会工作
/*
第一个参数:定时器对象
第二个参数:需要指定runloop的运行模式
*/
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
以上两种方式都会出现拖拽控件卡顿的现象
解决:1、将NSTimer换成UITrackingRunLoopMode或者NSRunLoopCommonModes模式下工作,这样就能解决以上问题
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
问题:如果有耗时操作的情况下,拖拽会出现主线程卡顿的情况
-(void)timerMethod{
[NSThread sleepForTimeInterval:1.0];//添加模拟耗时操作阻塞主线程
static int num = 0;
NSLog(@"%@,%d",[NSThread currentThread],num++);
}
解决2:将定时器放在子线程中处理
自定义一个HMPTread类集成NSTread,在子线程中开启runloop
HMPTread *tread = [[HMPTread alloc]initWithBlock:^{
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
NSLog(@"来了老弟");
}];
[tread start];
//注意 NSLog(@"来了老弟");这句是无法打印的,因为 [[NSRunLoop currentRunLoop] run];在子线程中死循环无法执行到这一步,而 [tread start];这句是在主线程中是可以执行到的
问题:创建的timer干不掉,无法控制timer
解决:通过一下方式外界也可以控制timer
#import "ViewController.h"
#import "HMPTread.h"
@interface ViewController ()
@property(nonatomic ,assign) BOOL finised;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_finised = YES;
[self timer1];
}
-(void)timer1
{
HMPTread *tread = [[HMPTread alloc]initWithBlock:^{
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
while(_finised){
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0.01]];
}
NSLog(@"来了老弟");
}];
[tread start];
}
-(void)timerMethod{
[NSThread sleepForTimeInterval:1.0];
static int num = 0;
NSLog(@"%@,%d",[NSThread currentThread],num++);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
_finised = NO;
}
@end
效果图

一个有意思的尝试
干掉主线程
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[NSThread exit];\\ 外界可以通过这个类方法进行对主线程干预,界面的控件拖拽不动
}
问题:我如何在子线程中用这个方法开启子线程的循环又能实现外界对timer的控制呢
[[NSRunLoop currentRunLoop] run];
可以通过一下方式,在timerMethod方法中干掉线程是干掉的是子线程,而主线程依然工作
#import "ViewController.h"
#import "HMPTread.h"
@interface ViewController ()
@property(nonatomic ,assign) BOOL finised;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_finised = NO;
[self timer1];
}
-(void)timer1
{
HMPTread *tread = [[HMPTread alloc]initWithBlock:^{
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
// while(_finised){
// [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0.01]];
// }
[[NSRunLoop currentRunLoop]run];
NSLog(@"来了老弟");
}];
[tread start];
}
-(void)timerMethod{
[NSThread sleepForTimeInterval:1.0];
if (_finised) {
[NSThread exit];
}
static int num = 0;
NSLog(@"%@,%d",[NSThread currentThread],num++);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
_finised = YES;
// [NSThread exit];
}

NSTimer定时器不精准
用GCD
- (void)viewDidLoad {
[super viewDidLoad];
//创建队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//创建gcd定时器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//设置定时器
dispatch_time_t start = DISPATCH_TIME_NOW;
dispatch_time_t interval = 1.0*NSEC_PER_SEC;
dispatch_source_set_timer(self.timer, start, interval, 0);
//设置回调
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"---------%@",[NSThread currentThread]);
});
//启动定时器
dispatch_resume(self.timer);
//子线程一旦开启,gcd会自动开启runloop
}
GCD会自动开启子线程的runloop,不用手动添加,这是GCD自动封装的
总结
1.保证程序不退出
2.负责监听所有的事件: 触摸(UI界面的处理),时钟,网络事件
NSDefaultRunLoopMode -- 时钟,网络事件
NSRunLoopCommonModes -- 用户交互(UI事件处理)
3.RunLoop 它还需要做一件事情 UI的绘制!! 在一次RunLoop循环中要绘制屏幕上所有的点!!!
网友评论