iOS常见功能demoiOS精华文章电商

iOS商城类商品抢购倒计时那点事

2016-06-12  本文已影响1405人  夜的v

一.之前电商项目做过的倒计时功能,笔记一下。
主要有两种思路:①:根据当前系统时间和抢购结束时间计算差值,计算倒计时。 ②:后台返回当前时间距离抢购结束时间的总秒数。根据秒数本地倒计时。(第二种方案更为准确,不受本地时间误差的影响)

代码如下:

方案一:根据当前系统时间和抢购结束时间计算差值,计算倒计时。在cell的m文件中计算。但是系统时间可能有误差,导致计时准确性有待提高。
1.cell的.h文件中定义属性字段@property(nonatomic,copy) NSString* endTime;
2:cell的.m文件中重写endTime的set方法,传入结束日期。
- (void)setEndTime:(NSString *)endTime 
{
    _endTime  =endTime;
    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshTime:) userInfo:nil repeats:YES];
    [self refreshTime:time];
    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSRunLoopCommonModes];
}
- (void)refreshTime:(NSTimer *)tm
{
    NSDate *currentDate =[NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit  unit = NSDayCalendarUnit | NSCalendarUnitHour | NSCalendarUnitMinute  | NSCalendarUnitSecond;
    NSDateComponents *commponent = [calendar components:unit fromDate:currentDate toDate:[HFTools getDateWithString:_endTime ] options:NSCalendarWrapComponents];
    
    NSDate *dt = [[HFTools getDateWithString:_endTime] earlierDate:currentDate];
    if([dt isEqualToDate:[HFTools getDateWithString:_endTime ]])
    {
        [tm invalidate];
        self.daoJiShiLabel.text = [NSString stringWithFormat:@"剩余0天 已结束"];
    }else
    {
        self.daoJiShiLabel.text = [NSString stringWithFormat:@"剩余%zd天 %02zd:%02zd:%02zd",commponent.day,commponent.hour,commponent.minute,commponent.second];    
    }
}
/******************
//其中[HFTools getDateWithString:_endTime ]方法为:
+(NSDate*)getDateWithString:(NSString *)time{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
     [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式 
    NSDate *date =[dateFormat dateFromString:time];
    return date;
}

方案二:

**方案二:后台给出距离抢购结束的总秒数,根据总秒数,计算倒计时。(计时准确.)。


![countDownPic.gif](http:https://img.haomeiwen.com/i1486049/1d675936523b06f0.gif?imageMogr2/auto-orient/strip)

代码:
#pragma mark - - 倒计时Timer..
- (void)initCountDown {
    for (NSInteger i = 0; i < self.dataArray.count; i++)
    {
        //将倒计时总秒数数组根据indexpath依次存入字典
        NSDictionary *CountDic = @{@"indexPath":[NSString stringWithFormat:@"%ld",i],@"lastTime": self.dataArray[i]};
        [self.countDownDataArray addObject:CountDic];
    }
    
    // 防止刷新界面的时候创建多个定时器,导致多个定时器一起倒计时。
    if (!self.MainTimer) {
        [self startTimer];
    }
}

//倒计时
- (void)startTimer
{
    self.MainTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:@"" repeats:YES];
    
    //如果不添加下面这条语句,在UITableView拖动的时候,会阻塞定时器的调用
    [[NSRunLoop currentRunLoop] addTimer:self.MainTimer forMode:UITrackingRunLoopMode];
}

//刷新时间
- (void)refreshLessTime
{
    NSUInteger time;
    for (int i = 0; i < self.countDownDataArray.count; i++) {
        time = [[[self.countDownDataArray objectAtIndex:i] objectForKey:@"lastTime"] integerValue];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[[[self.countDownDataArray objectAtIndex:i] objectForKey:@"indexPath"] integerValue] inSection:0];
        NSInteger oldTime;
        if (time == 0) {
            oldTime = 0;
        }else {
            oldTime = --time;
        }
        NSString *str;
        str = [NSString stringWithFormat:@"%@",[self lessSecondToDay:oldTime]];
        
        //根据indexpath取cell
        YMCountDownCell *cell = (YMCountDownCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        cell.countDownLabel.text = [self lessSecondToDay:oldTime];
        
        //将倒计时后的秒数存入数组,刷新数据源。
        NSDictionary *dic = @{@"indexPath": [NSString stringWithFormat:@"%ld",indexPath.row],@"lastTime": [NSString stringWithFormat:@"%ld",time]};
        [self.countDownDataArray replaceObjectAtIndex:i withObject:dic];
    }
}

//根据秒数计算剩余时间:天,小时,分钟,秒
- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
    NSUInteger day  = (NSUInteger)seconds/(24*3600);
    NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600;
    NSUInteger min  = (NSUInteger)(seconds%(3600))/60;
    NSUInteger second = (NSUInteger)(seconds%60);
    NSString *timeStr;
    if (seconds == 0) {
        timeStr = @"已结束";
        [self countDownFinished];
    }else {
        timeStr = [NSString stringWithFormat:@"%02zd天 %02zd:%02zd:%02zd",(unsigned long)day,(unsigned long)hour,(unsigned long)min,(unsigned long)second];
    }
    return timeStr;
}

// do something when the The countdown ends
- (void)countDownFinished
{
    
}


注意:倒计时开始会有一秒的刷新空档期,可以铺上倒计时数据防止倒计时UI一片白.(如果有刷新,刷新的时候要更新数据源 self.dataArray)。
我用的tableview,所以就在cell上铺。

NSInteger backTime = [self.dataArray[indexPath.row] integerValue];
    NSString *backStr  = [self lessSecondToDay:backTime];
    cell.countDownLabel.text = backStr;

最后附上方案二的github链接 倒计时Demo

ps:如果有好的思路欢迎拍砖交流哈。

上一篇下一篇

猜你喜欢

热点阅读