iOS时间分钟秒的倒计时
#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];
}
}
本文名称:iOS时间分钟秒的倒计时
网站网址:http://hbruida.cn/article/jdgjji.html