解决iOS程序UI主线程和定时器相互阻塞的问题

此文是接上文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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值