iOS开发中解决UIScrollView滚动时NSTimer失效的问题
2017-04-10 22:12
489 查看
我不知道大家在使用NSTimer的时候有没有发现如果我们滑动ScrollView,NSTimer就会失效。我曾经遇到过这样的问题,但需求是即使滑动scrollView,NSTimer也不能失效,于是就好好地研究了下这个问题。最终发现是NSTimer选择的一个模式的问题。
代码:
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSDefaultRunLoopMode];
我们将timer添加到当前的主线程中,并且选择NSDefaultRunLoopMode这个默认的模式。在选择这个默认的模式之后,如果我们不与UI进行交互那么NSTimer是有效的,如果我们与UI进行交互那么主线程runloop就会转到UITrackingRunLoopMode模式下,不能处理定时器,从而定时器失效。
让定时器不失效的方式有两种:1.改变runloop的模式(NSRunLoopCommonModes),无论用户是否与UI进行交互主线程的runloop都能处理定时器。2.开启一个新的线程,让定时器在新的线程中进行定义,这时定时器就会被子线程中的runloop处理。
代码:
改变runloop的模式:
self.timer = [NSTimertimerWithTimeInterval:1.0target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
// 添加到运行循环 NSRunLoopCommonModes:占位模式 主线程
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSRunLoopCommonModes]; //
如果不改变Mode模式在滑动屏幕的时候定时器就不起作用了
在子线程中定义定时器:
[NSThreaddetachNewThreadSelector:@selector(bannerStart)toTarget:selfwithObject:nil];
// 在子线程中定义定时器
- (void)bannerStart{
self.timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSDefaultRunLoopMode];
[[NSRunLoopcurrentRunLoop]
run];
}
- (void)updateTimer
{
NSLog(@"当前线程:%@",[NSThread
currentThread]);
NSLog(@"启动RunLoop后--%@",[NSRunLoop
currentRunLoop].currentMode);
NSLog(@"currentRunLoop:%@",[NSRunLoop
currentRunLoop]);
dispatch_async(dispatch_get_main_queue(), ^{
self.count ++;
NSString *timerText = [NSString
stringWithFormat:@"计时器:%ld",self.count];
self.timerLabel.text = timerText;
});
}
代码:
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSDefaultRunLoopMode];
我们将timer添加到当前的主线程中,并且选择NSDefaultRunLoopMode这个默认的模式。在选择这个默认的模式之后,如果我们不与UI进行交互那么NSTimer是有效的,如果我们与UI进行交互那么主线程runloop就会转到UITrackingRunLoopMode模式下,不能处理定时器,从而定时器失效。
让定时器不失效的方式有两种:1.改变runloop的模式(NSRunLoopCommonModes),无论用户是否与UI进行交互主线程的runloop都能处理定时器。2.开启一个新的线程,让定时器在新的线程中进行定义,这时定时器就会被子线程中的runloop处理。
代码:
改变runloop的模式:
self.timer = [NSTimertimerWithTimeInterval:1.0target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
// 添加到运行循环 NSRunLoopCommonModes:占位模式 主线程
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSRunLoopCommonModes]; //
如果不改变Mode模式在滑动屏幕的时候定时器就不起作用了
在子线程中定义定时器:
[NSThreaddetachNewThreadSelector:@selector(bannerStart)toTarget:selfwithObject:nil];
// 在子线程中定义定时器
- (void)bannerStart{
self.timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]
addTimer:self.timerforMode:NSDefaultRunLoopMode];
[[NSRunLoopcurrentRunLoop]
run];
}
- (void)updateTimer
{
NSLog(@"当前线程:%@",[NSThread
currentThread]);
NSLog(@"启动RunLoop后--%@",[NSRunLoop
currentRunLoop].currentMode);
NSLog(@"currentRunLoop:%@",[NSRunLoop
currentRunLoop]);
dispatch_async(dispatch_get_main_queue(), ^{
self.count ++;
NSString *timerText = [NSString
stringWithFormat:@"计时器:%ld",self.count];
self.timerLabel.text = timerText;
});
}
相关文章推荐
- iOS开发:解决UIScrollView不滚动的问题
- ios开发 解决ios6中UIScrollView不能滚动的问题
- iOS开发——解决UIScrollView控件移动错位和无法滚动
- UIScrollView 滚动视图—IOS开发
- iPhone开发之UIScrollView滚动问题
- 【iOS-Cocos2d游戏开发之十六】添加本地通知(UILocalNotification)以及添加系统组件滚动视图(UIScrollView)!【2011年11月15日更新】
- 解决ios6中UIScrollView不能滚动的问题
- IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决
- 打开键盘遮住View的问题解决方法-IOS开发
- 【iOS-Cocos2d游戏开发之七】添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题! 推荐
- UIScrollView 滚动视图—IOS开发
- 打开键盘遮住View的问题解决方法-IOS开发
- 解决UIScrollView 不能滚动的问题
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】
- UIScrollView 滚动视图—IOS开发
- IOS开发问题解决之UIAlertView调用UIActionSheet后灰色背景一直存在
- 使用Unity3D时,UIScrollView不滚动问题的解决方法
- UIScrollView 滚动视图—IOS开发
- iOS开发滚动视图UIScrollView
- IOS开发问题解决之UIAlertView调用UIActionSheet后灰色背景一直存在