简体   繁体   English

在iOS中启动计时器倒数

[英]Start timer countdown in iOS

I have a task in which I need to start timer countdown and I have days, hours, minutes and seconds but don't have any idea how to start timer with these entities. 我有一项任务需要启动计时器倒数,并且有几天,几小时,几分钟和几秒钟,但对如何使用这些实体启动计时器一无所知。 There are so many tutorials for counter but none of them were as per my requirement. 有太多的计数器教程,但都不是我的要求。 For example, I'll get 20 days, 10 hours, 40 minutes and 30 seconds as ending date and starting date from server and I need to start timer from these entities. 例如,我将从服务器获得20天,10小时,40分钟和30秒作为结束日期和开始日期,我需要从这些实体中启动计时器。 Please suggest me how to do that? 请建议我该怎么做?

You need to go with NSDateComponents 您需要使用NSDateComponents

NSDate* result;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:0];/ use your retrieved values
    [comps setHour:0];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    result = [gregorian dateFromComponents:comps];

Use the NSDate into the NSTimer 使用NSDate进入NSTimer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM