用nstimer实现倒计时

用nstimer实现倒计时
//
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; 

//
- (void)timerFireMethod:(NSTimer *)theTimer
{
    BOOL timeStart = YES;
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    NSDateComponents *endTime = [[NSDateComponents alloc] init];    //初始化目标时间...
    NSDate *today = [NSDate date];    //得到当前时间 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateString = [dateFormatter dateFromString:todate];
    NSString *overdate = [dateFormatter stringFromDate:dateString];
//    NSLog(@"overdate=%@",overdate);
    static int year;
    static int month;
    static int day;
    static int hour;
    static int minute;
    static int second;
    if(timeStart) {//从NSDate中取出年月日,时分秒,但是只能取一次
        year = [[overdate substringWithRange:NSMakeRange(0, 4)] intValue];
        month = [[overdate substringWithRange:NSMakeRange(5, 2)] intValue];
        day = [[overdate substringWithRange:NSMakeRange(8, 2)] intValue];
        hour = [[overdate substringWithRange:NSMakeRange(11, 2)] intValue];
        minute = [[overdate substringWithRange:NSMakeRange(14, 2)] intValue];
        second = [[overdate 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 *overTime = [cal dateFromComponents:endTime]; //把目标时间装载入date
    //用来得到具体的时差,是为了统一成北京时间
    unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:overTime options:0];
    NSString *t = [NSString stringWithFormat:@"%d", [d day]];
    NSString *h = [NSString stringWithFormat:@"%d", [d hour]];
    NSString *fen = [NSString stringWithFormat:@"%d", [d minute]];
    if([d minute] < 10) {
        fen = [NSString stringWithFormat:@"0%d",[d minute]];
    }
    NSString *miao = [NSString stringWithFormat:@"%d", [d second]];
    if([d second] < 10) {
        miao = [NSString stringWithFormat:@"0%d",[d second]];
    }
//    NSLog(@"===%@天 %@:%@:%@",t,h,fen,miao);
    [_longtime setText:[NSString stringWithFormat:@"%@天 %@:%@:%@",t,h,fen,miao]];
    if([d second] > 0) {
        //计时尚未结束,do_something
//        [_longtime setText:[NSString stringWithFormat:@"%@:%@:%@",d,fen,miao]];
    } else if([d second] == 0) {
        //计时结束 do_something 

    } else{
//计时器失效
        [theTimer invalidate];
    } 

}
时间: 2024-08-03 02:28:18

用nstimer实现倒计时的相关文章

iOS开发过程中的小技巧

转载自:http://blog.csdn.net/a451493485/article/details/8598839 iphone程序中实现截屏的一种方法在iphone程序中实现截屏的一种方法: //导入头文件#import QuartzCore/QuartzCore.h//将整个self.view大小的图层形式创建一张图片image UIGraphicsBeginImageContext(self.view.bounds.size):[self.view.layer renderInCont

NSTimer 实现到一个指定时间(年、月、日)的倒计时

//模仿抢购到记时 //用指定格式dateFormatter构建date实例,获取构建的这个时间与当前时间[NSDate date]的时间差(秒数) - (NSString *)countDown{ NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@”yyyy-MM-dd HH-mm-ss”]; NSDate *date = [d

iOS 倒计时NSTimer

项目中可能会遇到有些倒计时的地方 比方 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就须要展示一个倒计时 详细实现方式是使用了iOS 自带的 NSTimer 上代码 首先新建 int secondsCountDown; //倒计时总时长 NSTimer *countDownTimer; UILabel *labelText; 然后详细实现 //创建UILabel 加入到当前view labelText=[[UILabel alloc]initWithFrame:CGRec

NSTimer实现读秒、倒计时等周期性操作

self.timerSchedule = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(spinPicker) userInfo:nil repeats:YES]; 该类函数将返回一个NSTimer的实例,并且按照scheduledTimerWithTimeInterval所设定的周期(秒)调用selector所指定的函数,如需循环调用则要将repeats的参数指定为YES. 判断NSTime

iOS:三种常见计时器(NSTimer、CADisplayLink、dispatch_source_t)的使用

一.介绍 在iOS中,计时器是比较常用的,用于统计累加数据或者倒计时等,例如手机号获取验证码.计时器大概有那么三种,分别是:NSTimer.CADisplayLink.dispatch_source_t 二.使用 @property (strong,nonatomic)NSTimer *timer; @property (strong,nonatomic)CADisplayLink *displaylinkTimer; @property (strong,nonatomic)dispatch_s

替代NSTimer的block计时器!! !

既然NSTimer容易造成内存泄露,那就给初出茅庐的小程序员们带来一个惊喜吧! 话不多说上代码: LIST -(void)startTime{ timeout_int=0; dispatch_queue_t queue_global =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYP

iOS 商品倒计时 限时特价 限时优惠 功能的封装

最近项目中多个页面用到了 商品特价倒计时的功能  为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @interface STGoodsOrientationPrice : STBaseModel /**定向价**/ @property (nonatomic, copy) NSString *price; /**定向价开始时间**/ @property (nonatomic, copy) NSStr

NSTimer的使用之iphone计时器功能模仿

1 程序运行效果: 1.1程序启动第一个界面,等待用户选择倒计时时间 1.2用户选择好倒计时时间后,开始倒计时 1.3接受用户取消.暂停.继续等中断 2 设计过程 2.1 拖出来一个日期拾取器控件,两个按钮,关联属性变量,然后添加辅助属性变量,如下: 1 @interface ViewController () 2 3 @property (weak, nonatomic) IBOutlet UIDatePicker *timePicker; 4 @property (weak, nonatom

[非凡程序员]倒计时 uiimage uiDatePicker NSDate

//倒计时 @implementation ViewController - (void)viewDidLoad {     [super viewDidLoad];     [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(jishi) userInfo:nil repeats:YES]; } - (void)didReceiveMemoryWarning {     [super didRe