iOS实现倒计时显示 时 分 秒

1.创建一个类继承自UILabel.(用来显示 时 分 秒)
.h文件
#import

@interface TimeLable : UILabel

@property (nonatomic,assign)NSInteger second;
@property (nonatomic,assign)NSInteger minute;
@property (nonatomic,assign)NSInteger hour;

@end
.m文件
#import "TimeLable.h"
@interface TimeLable ()
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation TimeLable

- (id)initWithFrame:(CGRect)frame{
       self = [super initWithFrame:frame];
       if (self) {
               self.textAlignment = NSTextAlignmentCenter;
               self.timer = [NSTimer scheduledTimerWithTimeIn terval:1 target:self selector:@selector(timeHeadle) userInfo:nil repeats:YES];
       }
       return self;
}

- (void)timeHeadle{
     
       self.second--;
       if (self.second==-1) {
               self.second=59;
               self.minute--;
               if (self.minute==-1) {
                       self.minute=59;
                       self.hour--;
               }
       }
     
       self.text = [NSString stringWithFormat:@"%ld:%ld:%ld",(long)self.hour,(long)self.minute,(long)self.second];
       if (self.second==0 && self.minute==0 && self.hour==0) {
               [self.timer invalidate];
               self.timer = nil;
       }
}

2.在需要倒计时器的类中导入头文件即可使用,示例:

  TimeLable *lable = [[TimeLable alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
       lable.hour = 10;
       lable.minute = 38;
       lable.second = 29;

    [self.view addSubview:lable];


-----------------------分割线----------------------------

整个label内部倒计时所显示的内容可以看做一个字符串

头部代码
#import "YQHomeViewController.h"

@interface YQHomeViewController ()

//将sb中的label进行拖拽过来
@property (weak, nonatomic) IBOutlet UILabel *timeLbl;

//创建定时器(因为下面两个方法都使用,所以定时器拿出来设置为一个属性)
@property(nonatomic,strong)NSTimer*countDownTimer;

@end


@implementation YQHomeViewController

//倒计时总的秒数
static NSInteger  secondsCountDown = 86400;

- (void)viewDidLoad {
    [super viewDidLoad];

实现步骤:

1.设置定时器(给定24小时)

一个定时器,定时器里面做设置间隔多少时间执行什么方法(countDownAction)

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置定时器
   _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
    //启动倒计时后会每秒钟调用一次方法 countDownAction

    //设置倒计时显示的时间
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];//时
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];//分
    NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];//秒
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    self.timeLbl.text = [NSString stringWithFormat:@"倒计时   %@",format_time];
    //设置文字颜色
    self.timeLbl.textColor = [UIColor  blackColor ];
    [self.view addSubview:_timeLbl];

}
2.方法(countDownAction)(来自创建定时器时需要执行的方法)内部:总秒数递减

1.先递减
2.给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示

//实现倒计时动作
-(void)countDownAction{
    //倒计时-1
    secondsCountDown--;

    //重新计算 时/分/秒
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];

    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];

    NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];

    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    //修改倒计时标签及显示内容
    self.timeLbl.text=[NSString stringWithFormat:@"实训倒计时   %@",format_time];


    //当倒计时到0时做需要的操作,比如验证码过期不能提交
    if(secondsCountDown==0){

        [_countDownTimer invalidate];
    }

    }

屏幕输出结果:


程序输出结果:灰色的两个scrollView可以忽略,不要让它干扰到你

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值