我是靠谱客的博主 忧心黑裤,这篇文章主要介绍iOS实现倒计时显示 时 分 秒,现在分享给大家,希望可以做个参考。

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

.h文件

//
//  TimeLable.h
//  timer
//
//  Created by limin on 17/8/15.
//  Copyright © 2017年 none. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TimeLable : UILabel
@property (nonatomic,assign)NSInteger second;
@property (nonatomic,assign)NSInteger minute;
@property (nonatomic,assign)NSInteger hour;
@end
.m文件

//
//  TimeLable.m
//  timer
//
//  Created by limin on 17/8/15.
//  Copyright © 2017年 none. All rights reserved.
//

#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 scheduledTimerWithTimeInterval: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--;
        }
    }
    if (self.hour>0) {
        self.text = [NSString stringWithFormat:@"%.2ld:%.2ld:%.2ld",(long)self.hour,(long)self.minute,(long)self.second];
    }else if (self.hour==0) {
        self.text = [NSString stringWithFormat:@"%.2ld:%.2ld",(long)self.minute,(long)self.second];
    }else if (self.minute==0)
    {
        self.text = [NSString stringWithFormat:@"%.2ld",(long)self.second];
    }
    
    if (self.second==0 && self.minute==0 && self.hour==0) {
        [self.timer invalidate];
        self.timer = nil;
    }
}


@end

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

#import "TimeLable.h"
- (void)viewDidLoad {
    [super viewDidLoad];
    TimeLable *lable = [[TimeLable alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    lable.hour = 10;
    lable.minute = 1;
    lable.second = 00;
    [self.view addSubview:lable];
}




最后

以上就是忧心黑裤最近收集整理的关于iOS实现倒计时显示 时 分 秒的全部内容,更多相关iOS实现倒计时显示内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部