這段時間寫公司的一個外包項目,需要用到倒計時:需要顯示時分秒,通過在網上搜集資料,找到了2中方法,我把這兩種方法結合起來,可以很好的滿足這個需求:
1、創建一個類繼承自UIlabel,用來展示時分秒的
.h文件
#import <UIKit/UIKit.h>
@interface TimerLab : UILabel
@property (nonatomic,assign)NSInteger second;
@property (nonatomic,assign)NSInteger minute;
@property (nonatomic,assign)NSInteger hour;
@end
.m文件
#import "TimerLab.h" @interface TimerLab () @property (nonatomic, strong)NSTimer *timer; @end @implementation TimerLab - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.textAlignment = NSTextAlignmentCenter; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeHeadle) userInfo:nil repeats:YES]; } return self; } - (void)timeHeadle{ self.second--; if (self.second==-1) { self.second=59; self.minute--; if (self.minute==-1) { self.minute=59; self.hour--; } } if (self.hour < 0 || self.minute < 0 || self.second < 0) { self.text = @"倒計時結束需要顯示的字樣"; [self.timer invalidate]; self.timer = nil; }else { self.text = [NSString stringWithFormat:@"%ld:%ld:%ld",(long)self.hour,(long)self.minute,(long)self.second]; } if (self.second==0 && self.minute==0 && self.hour==0) { [self.timer invalidate]; self.timer = nil; } } @end
2、我是在cell里面顯示的,(這里注意,不能用xib或者SB拖得,那樣不行,必須得是手寫,具體是因為使用拖得空間,沒有走alloc很重,所以在控制器上顯示會有問題)
(1)在項目里面具體實現下面這2個方法
#pragma mark 每個cell倒計時的方法/*-------------------------------------------------------*/ - (NSString*)remainingTimeMethodAction:(long long)endTime { //得到當前時間 NSDate *nowData = [NSDate date]; NSLog(@"---當前時間:%@",nowData); NSDate *endData=[NSDate dateWithTimeIntervalSince1970:endTime]; NSLog(@"----結束時間%@",endData); NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ]; NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit; NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:nowData toDate: endData options:0]; NSInteger Hour = [cps hour]; NSInteger Min = [cps minute]; NSInteger Sec = [cps second]; NSInteger Day = [cps day]; NSInteger Mon = [cps month]; NSInteger Year = [cps year]; NSLog( @" From Now to %@, diff: Years: %ld Months: %ld, Days; %ld, Hours: %ld, Mins:%ld, sec:.%ld", [nowData description], (long)Year, (long)Mon, (long)Day, (long)Hour, (long)Min,(long)Sec ); // NSString *countdown = [NSString stringWithFormat:@"還剩: %zi天 %zi小時 %zi分鍾 %zi秒 ", Day,Hour, Min, Sec]; NSString *countdown = [NSString stringWithFormat:@"%zi:%zi:%zi",Hour, Min, Sec]; NSLog(@"0000%@",countdown); if (Sec<0 && Min<0 && Hour<0) { countdown=[NSString stringWithFormat:@"進行中"]; } return countdown; } //時間轉化時間戳 -(NSString *)checksting:(NSString *)datas { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate *date = [formatter dateFromString:datas]; NSString *timerStrs = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]]; return timerStrs; } #pragma mark/*------------------------------------------------------------------------- */
3,cell里面的具體賦值操作
//1、 獲得到期時間串 NSString *timSts = [NSString stringWithFormat:@"%@",model.time2]; //2、 轉化成到時時間戳,並傳進對比方法里面 NSString *timerStrs = [NSString stringWithFormat:@"%@",[self remainingTimeMethodAction:[[self checksting:timSts] longLongValue]]]; //3、 分割字符串,取出時分秒 NSArray *ary = [timerStrs componentsSeparatedByString:@":"]; NSLog(@"ary is %@",timerStrs); if (ary.count == 3) { cell.YB_dqTimeLab.hour = [[ary objectAtIndex:0] integerValue]; cell.YB_dqTimeLab.minute = [[ary objectAtIndex:1] integerValue]; cell.YB_dqTimeLab.second = [[ary objectAtIndex:2] integerValue]; }else if(ary.count == 1) { cell.YB_dqTimeLab.hour = 0; cell.YB_dqTimeLab.minute = 0; cell.YB_dqTimeLab.second = 0; }
我是用了別人寫好的倒計時顯示用的自定義的label,然后使用時間比對的方法,算出時間差,然后進行倒計時的操作!如有理解不到的地方
還希望大家指正!