解决iOS程序UI主线程和定时器相互阻塞的问题
来源:互联网 发布:淘宝联盟福利红包 编辑:程序博客网 时间:2024/06/11 23:27
此文是接上文http://www.cnblogs.com/zzltjnh/archive/2013/05/15/3080058.html问题解决之后遇到的一个新问题,我的页面上有一个UIScrollView和一个定时器用来记录当前考试模式下的剩余时间,问题出现了:当我滑动滚动试图时,定时器的方法便不在运行(即被UI主线程阻塞)。google一下找到了解决办法:将定时器放在非主线程中执行将更新UI的操作放到主线程,这样UI主线程和定时器就能互不干扰的相互工作了,以下是主要代码:
#import "CountdownTool.h"@interface CountdownTool(){ UILabel *_lblShow; NSTimer *_timer;}@property (nonatomic, assign) NSInteger hour;@property (nonatomic, assign) NSInteger minute;@property (nonatomic, assign) NSInteger second;@property (nonatomic, copy) NSString *strHour;@property (nonatomic, copy) NSString *strMinute;@property (nonatomic, copy) NSString *strSecond;@property (nonatomic, assign) NSInteger totalSeconds;@end@implementation CountdownTool@synthesize hour = _hour;@synthesize minute = _minute;@synthesize second = _second;@synthesize totalSeconds = _totalSeconds;- (void)dealloc{ [_lblShow release]; [_strHour release]; [_strMinute release]; [_strSecond release]; [super dealloc];}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { _lblShow = [[UILabel alloc] initWithFrame:self.bounds]; _lblShow.backgroundColor = [UIColor clearColor]; _lblShow.font = [UIFont systemFontOfSize:15]; _lblShow.textColor = [UIColor yellowColor]; _lblShow.textAlignment = NSTextAlignmentCenter; _lblShow.numberOfLines = 1; [self addSubview:_lblShow]; } return self;}- (id)initWithFrame:(CGRect)frame andMinutesNum:(NSInteger)minute{ if (self = [self initWithFrame:frame]) { self.totalSeconds = minute * 60; //多线程启动定时器 [NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil]; } return self;}- (void)startTimer{ _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFire) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] run];}- (void)handleWithTotalSeconds{ self.hour = _totalSeconds/3600; self.minute = _totalSeconds%3600/60; self.second = _totalSeconds%3600%60; if (_hour <= 0) { _lblShow.text = [NSString stringWithFormat:@"%@:%@",_strMinute,_strSecond]; }else{ _lblShow.text = [NSString stringWithFormat:@"%@:%@:%@",_strHour,_strMinute,_strSecond]; }}- (void)setHour:(NSInteger)hour{ _hour = hour; if (_hour < 10) { self.strHour = [NSString stringWithFormat:@"0%d",_hour]; }else{ self.strHour = [NSString stringWithFormat:@"%d",_hour]; }}- (void)setMinute:(NSInteger)minute{ _minute = minute; if (_minute < 10) { self.strMinute = [NSString stringWithFormat:@"0%d",_minute]; }else{ self.strMinute = [NSString stringWithFormat:@"%d",_minute]; }}- (void)setSecond:(NSInteger)second{ _second = second; if (_second < 10) { self.strSecond = [NSString stringWithFormat:@"0%d",_second]; }else{ self.strSecond = [NSString stringWithFormat:@"%d",_second]; }}- (void)setTotalSeconds:(NSInteger)totalSeconds{ _totalSeconds = totalSeconds; [self performSelectorOnMainThread:@selector(handleWithTotalSeconds) withObject:nil waitUntilDone:YES];}- (void)timerFire{ if (_totalSeconds == 0) { [_timer invalidate]; return; } self.totalSeconds -= 1;}@end
0 0
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- 解决UI主线程阻塞原因
- 异步加载图片“阻塞”UI主线程问题的解决方案
- 解决sleep延时阻塞程序主线程,造成程序未响应问题方案
- 解决MBProgressHUD阻塞主线程的方法
- IOS 更新UI的主线程方法
- 解决使用Delphi的Thread时主线程阻塞
- iOS主线程更新UI
- iOS 主线程更新UI
- ios主线程更改UI
- MFC主线程使用WaitForSingleObject阻塞的问题
- 【Android和iOS】快速切换到主线程更新UI
- Qt 定时器signal/slot阻塞主线程界面
- 不阻塞UI的跑马灯,解决跑马灯和动画冲突问题
- php 301重定向解决办法,超详细(附案例图)
- android 怎么关闭其他应用程序
- AndroidPN源码分析(包含client和server)
- Js apply 方法 详解
- iPhone retina屏的系统字体对应像素高
- 解决iOS程序UI主线程和定时器相互阻塞的问题
- ****Android动画效果translate、scale、alpha、rotate详解
- 小米HBase实践_刘绍辉
- 杂项
- 三星i9100 CM9 rom Unable to open log device '/dev/log/main'的解决办法
- 从github上clone子分支
- Mysql 大表查询效率
- Dictionary,List<>,Hashtable比较
- Play framework学习(导入默认数据)