NSTimer 倒计时自动登录,每秒更新UI

来源:互联网 发布:unity3d柔体 编辑:程序博客网 时间:2024/05/22 16:50

NSTimer 倒计时及更新UI


描述:

使用NSTimer实现倒计时自动登录功能。


解决:

//创建计时器。每秒调用updateUI方法。NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateUI:) userInfo:nil repeats:YES];-(void)updateUI:(id)sender{    if((self.time--)>0)    {        NSLog(@"timer:%d",self.time);        //更新UI        self.timeLabel.text = [NSString stringWithFormat:@"%ds后自动跳转",self.time];        //timelabel使用了sizeToFit,这里也要再使用一次。        [self.timeLabel sizeToFit];                if (self.time == 0)        {            //登录            [self pressLoginBtnTouched:self];        }    }    else    {       //倒计时结束 需要关闭计时器        [self.timer invalidate];    }}


0 0