[iOS][Runloop]:定时器事件和拖拽事件不冲突解决方案

Snip20181019_1.png

场景:当主线程默认的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

效果图

Snip20181019_3.png
一个有意思的尝试

干掉主线程

-(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];
   
}
Snip20181019_4.png
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循环中要绘制屏幕上所有的点!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 说明iOS中的RunLoop使用场景1.保持线程的存活,而不是线性的执行完任务就退出了<1>不开启RunLoop的...
    野生塔塔酱阅读 6,916评论 15 109
  • Runloop 是和线程紧密相关的一个基础组件,是很多线程有关功能的幕后功臣。尽管在平常使用中几乎不太会直接用到,...
    jackyshan阅读 10,001评论 10 75
  • 基本概念 进程 进程是指在系统中正在运行的一个应用程序,而且每个进程之间是独立的,它们都运行在其专用且受保护的内存...
    小枫123阅读 1,000评论 0 1
  • 什么是RunLoop Run Loop是一让线程能随时处理事件但不退出的机制。RunLoop 实际上是一个对象,这...
    ikonan阅读 671评论 1 3
  • iOS刨根问底-深入理解RunLoop 2017-05-08 10:35 by KenshinCui 概述 Run...
    mengjz阅读 1,631评论 1 10

友情链接更多精彩内容