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

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

作者: 阿不不不不 | 来源:发表于2018-10-19 15:54 被阅读20次
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循环中要绘制屏幕上所有的点!!!

相关文章

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

    场景:当主线程默认的runloop模式是 NSDefaultRunLoopMode模式,而textview控件拖拽...

  • iOS runloop 机制与使用

    runloop是运行循环,iOS中,APP处于随时待命的状态,处理包括:触摸事件、UI刷新事件、定时器事件、Sel...

  • RunLoop

    RunLoop RunLoop就是运行循环,处理app中的各种事件(比如触摸事件,定时器事件,Selector事件...

  • iOS底层原理-Runloop

    Runloop Runloop作用: 保持程序的持续运行 处理程序的各种事件(触摸事件、定时器事件等) 节约CPU...

  • iOS中的Runloop

    摘要 本文介绍iOS中的事件循环Runloop; Runloop 是什么 Runloop是事件接收和分发机制的一个...

  • RunLoop与多线程的原理和使用

    RunLoop1.事件接收和分发机制的实现2.处理App中的各种事件(比如触摸事件、定时器事件、selector事...

  • runLoop

    Runloop 保持程序的持续运行 处理APP中的各种事件(比如触摸事件,定时器事件,selector事件) 节省...

  • RunLoop学习笔记

    一.主线程RunLoop:保证App不退出 负责监听所有的事件(触摸事件)(网络事件)(定时器事件)无事件 Run...

  • Runloop2_模式和监听事件

    Runloop 官方文档 Runloop和定时器的关系 上篇文章说过,Runloop监听所有的事件中包括 时钟 N...

  • 可全局拖拽、点击的View

    先上图 这个效果和IOS的Assistive Touch效果类似,可以实现全局拖拽,设置点击事件,并且点击和拖拽不...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/zvzyzftx.html