#import "ViewController.h"
#import "QSSDateHelper.h"
@interface ViewController ()
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,assign) BOOL isTouch;
@property (nonatomic,assign) NSInteger time;
@property (nonatomic,assign) int year;
@property (nonatomic,assign) int mouth;
@property (nonatomic,assign) int day;
@property (nonatomic,assign) int hour;
@property (nonatomic,assign) int minute;
@property (nonatomic,assign) int second;
@property (nonatomic,assign) int myTouchSecond;
@property NSDate *touchDate;
@property NSCalendar *cal;
@property NSTimer *timer ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initBtn];
self.isTouch=NO;
}
- (void)dealloc{
[self.timer invalidate];
self.timer=nil;
self.time=0;
}
- (void)initBtn{
self.btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.btn setTitle:@"倒计时" forState:UIControlStateNormal];
[self.btn setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:self.btn];
[self.btn addTarget:self action:@selector(releaseTime) forControlEvents:UIControlEventTouchUpInside];
}
- (void)timerFireMethod:(NSTimer *)timer{
//NSDate *dd = [NSDate dateWithTimeIntervalSince1970:self.myTouchSecond];
int targetSecond = self.myTouchSecond + 10*60;//目标的时间
//把目标的秒数转化为固定格式的秒数
NSDate *dd = [NSDate dateWithTimeIntervalSince1970:targetSecond];
unsigned int time2=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSCalendar *cal=[NSCalendar currentCalendar];
NSDateComponents *t2=[cal components:time2 fromDate:dd];
//==================开始定时的时间=====================
unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *t=[self.cal components:time fromDate:self.touchDate]; //开始定时的时间
NSLog(@"开始倒计时的时间:%d天%d小时%d分钟%d秒",(int)t.day,(int)t.hour,(int)t.minute,(int)t.second);
//==================目标时间==========================
NSLog(@"目标时间:%d天%d小时%d分钟%d秒",(int)t2.day,(int)t2.hour,(int)t2.minute,(int)t2.second);
//目标时间
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:t2.year];
[components setMonth:t2.month];
[components setDay:t2.day];
[components setHour:t2.hour];
[components setMinute:t2.minute];
NSLog(@"minute +++++ 10 %d",self.minute+10);
[components setSecond:self.second];
unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
//NSDate *fireDate = [calendar dateFromComponents:components];//目标时间
NSDate *nowhhhhh=[NSDate date];
NSDateComponents *d = [calendar components:unitFlags fromDate:nowhhhhh toDate:dd options:0];//计算时间差
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%d分钟%d秒",(int)[d minute],(int)[d second]] message:@"倒计时" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
alert.delegate=self;
[alert show];
}
- (void)releaseTime{
self.time++;
self.isTouch=YES;
if (self.time==1){
NSLog(@"开始倒计时");
//当前时间 只调用一次这个方法。。。
NSDate *now=[NSDate date];
NSCalendar *cal=[NSCalendar currentCalendar];
unsigned int time=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents *t=[cal components:time fromDate:now];
self.year=(int)[t year];
self.mouth=(int)[t month];
self.day=(int)[t day];
self.hour=(int)[t hour];
self.minute=(int)[t minute];
self.second=(int)[t second];
NSTimeInterval time2=[[NSDate date] timeIntervalSince1970];
self.myTouchSecond = time2;
NSLog(@"date\n%d", self.myTouchSecond);
self.touchDate=now;
self.cal=cal;
}else{
self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
[self.timer setFireDate:[NSDate distantPast]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end