您好,登錄后才能下訂單哦!
#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];
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。