NSTimer和拖拽冲突的问题解决

问题:
  • 比如我们界面顶部有个无限轮播图,而下面有个tableView,当我们上下滑动tableView时,会发现定时器停止工作了。这是为什么呢?
问题原因:
  • 这是由于NSTimer的RunLoop运行模式和tableView的运行模式不同的,下面来看看解决方案
#方法调用:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     //第一种方法创建NSTimer
    //[self timer1];
    
    //第二种方法创建NSTimer
    //[self timer2];
 #pragma mark -- 问题又来了如果我的定时器是在子线程中呢?
    //开个子线程
    [NSThread detachNewThreadSelector:@selector(timer3) toTarget:self withObject:nil];
    //这样是不会运行的,原因看方法里的解释
}

首先看看NSTimer的两种创建模式
  • timerWithTimeInterval
-(void)timer1
{
    //1.创建定时器—用此方法创建定时器是需要手动将定时器添加到RunLoop中的
   NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //2.添加定时器到RunLoop中,指定当前的运行模式为默认的
   // [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

#如果定时器的运行模式是:NSDefaultRunLoopMode,如果界面上又有继承于UIScrollView的控件,当我们拖拽时,就自动进入RunLoop的界面追踪模式了,而NSTimer的运行模式又是默认的运行模式,所以NSTimer就停止运行了

#解决方案一:需求是只有拖拽时,定时器才工作
    //只需设置NSTimer的运行模式为追踪模式
   // [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
 
 #解决方案二: 需求是无论是拖拽还是没有拖拽都要运行NSTimer
    //方法1.最笨的方法是将Timer添加两次--就是两种运行模式都添加一次
    // [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
     //[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode]
    
    //方法2.NSRunLoopCommonModes用这种运行模式就相当于上面两个都添加了
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)run
{
   //打印当前线程和当前线程的RunLoop模式
    NSLog(@"run --- %@ --- %@",[NSThread currentThread],[NSRunLoop currentRunLoop].currentMode);
    
}

  • scheduledTimerWithTimeInterval
-(void)timer2
{
  //用这种方法创建定时器--该方法内部会自动添加到RunLoop模式中,并且运行模式是默认模式
  NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    # 如果是用这种方法创建的定时器想要在拖拽的时候不影响的话
    //拿到定时器对象给它重新设置一下运行模式就好了
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

  • 子线程—scheduledTimerWithTimeInterval
-(void)timer3
{
  //为什么在子线程中就不会运行呢--因为子线程的RunLoop需要我们自己创建
   //创建子线程RunLoop
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
//创建定时器   
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
  //开启RunLoop(子线程的RunLoop创建好后,默认是不开启的)
    [currentRunLoop run];
}

小结: 解决NSTimer和控件拖拽冲突的两个方法

  • 将NSTimer的RunLoop运行模式设置为NSRunLoopCommonModes
  • 将NSTimer的运行放入子线程中

你可能感兴趣的:(NSTimer和拖拽冲突的问题解决)