你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 實現倒計時

iOS 實現倒計時

編輯:IOS開發綜合
#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

- (void)dealloc{
[self.timer invalidate];
self.timer=nil;
self.time=0;
}

- (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
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved