iOS 时间分钟秒的倒计时

发布时间:2020-04-07 22:03:26 作者:大头狼小鬼
来源:网络 阅读:432

#import "ViewController.h"


@interface ViewController ()

{

    BOOL timeStart;

    NSMutableArray *savaTimeArry;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

    timeStart = YES;


}



- (void)timerFireMethod:(NSTimer *)theTimer

{

    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象

    NSDateComponents *endTime = [[NSDateComponents alloc] init];    //初始化目标时间...

    NSDate *today = [NSDate date];    //得到当前时间

    

    NSDate *date = [NSDate dateWithTimeInterval:117000 sinceDate:today];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateString = [dateFormatter stringFromDate:date];

    

    static int year;

    static int month;

    static int day;

    static int hour;

    static int minute;

    static int second;

    if(timeStart)

    {//NSDate中取出年月日,时分秒,但是只能取一次

        year = [[dateString substringWithRange:NSMakeRange(0, 4)] intValue];

        month = [[dateString substringWithRange:NSMakeRange(5, 2)] intValue];

        day = [[dateString substringWithRange:NSMakeRange(8, 2)] intValue];

        hour = [[dateString substringWithRange:NSMakeRange(11, 2)] intValue];

        minute = [[dateString substringWithRange:NSMakeRange(14, 2)] intValue];

        second = [[dateString substringWithRange:NSMakeRange(17, 2)] intValue];

        timeStart= NO;

    }

    

    [endTime setYear:year];

    [endTime setMonth:month];

    [endTime setDay:day];

    [endTime setHour:hour];

    [endTime setMinute:minute];

    [endTime setSecond:second];

    NSDate *todate = [cal dateFromComponents:endTime]; //把目标时间装载入date

    

    //用来得到具体的时差,是为了统一成北京时间

    unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit;

    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];

    NSString *fen = [NSString stringWithFormat:@"%ld", [d minute]];

    NSString *hou = [NSString stringWithFormat:@"%ld",[d hour]];

    if([d hour]<10)

    {

        hou = [NSString stringWithFormat:@"0%ld",[d hour]];

    }

    

    if([d minute] < 10) {

        fen = [NSString stringWithFormat:@"0%ld",[d minute]];

    }

    NSString *miao = [NSString stringWithFormat:@"%ld", [d second]];

    if([d second] < 10) {

        miao = [NSString stringWithFormat:@"0%ld",[d second]];

    }

    

    if([d second] > 0)

    {

        savaTimeArry = [NSMutableArray array];

        NSString *shouTimer= [NSString stringWithFormat:@"%@%@%@",hou,fen,miao];

        NSString *b1= [shouTimer substringWithRange:NSMakeRange(0,1)];

        NSString *b2= [shouTimer substringWithRange:NSMakeRange(1,1)];

        NSString *b3= [shouTimer substringWithRange:NSMakeRange(2,1)];

        NSString *b4= [shouTimer substringWithRange:NSMakeRange(3,1)];

        NSString *b5= [shouTimer substringWithRange:NSMakeRange(4,1)];

        NSString *b6= [shouTimer substringWithRange:NSMakeRange(5,1)];

        [savaTimeArry addObject:b1];

        [savaTimeArry addObject:b2];

        [savaTimeArry addObject:b3];

        [savaTimeArry addObject:b4];

        [savaTimeArry addObject:b5];

        [savaTimeArry addObject:b6];

    

        [self showtimeView];

        

    } else if([d second] == 0)

    {

        

        //计时1分钟结束,do_something

        

    } else

    {

        [theTimer invalidate];

    }

    

}



- (void)showtimeView

{

    for(int i = 0 ;i <6;i++)

    {

        UIButton *butom = [UIButton buttonWithType:UIButtonTypeCustom];

        butom.frame = CGRectMake(10+10+i*10+2*i+(int)(i/2)*5, 100, 10, 20);

        butom.backgroundColor = [UIColor blackColor];

        [butom setTitle:[savaTimeArry objectAtIndex:i] forState:UIControlStateNormal];

        [butom setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        butom.titleLabel.font = [UIFont systemFontOfSize:14.0f];

        [self.view addSubview:butom];

    }

}


推荐阅读:
  1. ios如何实现倒计时器
  2. 疯狂ios讲义疯狂连载之日期选择器(UIDatePicker)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:12月技术考核:Windows系统故障排查

下一篇:leetCode 342. Power of Four 位运算

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》