美文网首页
cell上倒计时

cell上倒计时

作者: 司徒新新 | 来源:发表于2019-01-02 12:02 被阅读10次

因为商城项目内有秒杀和限时购的需求,需要每个cell上都有一个倒计时,所以写了一个用一个计时器 控制所有cell的倒计时时间控件;


////  XinxinTimer.m

//  Created by 新新 on 2017/12/6.

//  Copyright © 2017年 com.hong5.ios. All rights reserved.

//#import "XinxinTimer.h"

@implementation XinxinTimer{

    dispatch_source_t           _timer;   

    UITableView         *       _tableView;

}

- (instancetype)initWithTableView:(UITableView*)tableView dataArray:(NSArray*)dataArray {    self = [super init];    if (self) {       

_tableView = tableView;       

_dataArray = [NSMutableArray arrayWithArray:dataArray];       

[self destoryTimer];       

[self countDownWithTabelView:_tableView dataArray:_dataArray];   

}       

return self;

}

#pragma mark -  传入数据 开始倒计时

- (void)countDownWithTabelView:(UITableView*)tableView dataArray:(NSArray*)dataArray {     

  _tableView = tableView;   

_dataArray= [NSMutableArray arrayWithArray:dataArray];  

 if (_timer==nil) {       

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);     

_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行        dispatch_source_set_event_handler(_timer, ^{           

dispatch_async(dispatch_get_main_queue(), ^{                             

if (dataArray.count > 0) {               

 NSArray  *cells = tableView.visibleCells; //取出屏幕可见ceLl             

      for (UITableViewCell * cell in cells) {                   

        NSString* tempEndTime;                     

      if ([dataArray[0] isKindOfClass:[NSArray class]]) {                 

          NSInteger section = cell.tag / 1000;                   

        NSInteger row = cell.tag % 1000;                       

    tempEndTime = dataArray[section][row];                       

}else {                     

      tempEndTime = dataArray[cell.tag];                   

    }                   

    for (UIView * labText in cell.contentView.subviews) {               

            if (labText.tag == 13141516) {           

                    UILabel * textLabel = (UILabel *)labText;                         

                      NSInteger endTime = tempEndTime.longLongValue; //+ _less;             

                      textLabel.text = [XinxinTimer getNowTimeWithString:endTime];               

            }                        }                    }                }                        });        });     

  dispatch_resume(_timer); // 启动定时器    }  

 }

#pragma mark -  滑动过快的时候不会闪

- (NSString *)countDownWithIndexPath:(NSIndexPath *)indexPath{ 

  NSString* tempEndTime;   

if ([_dataArray[0] isKindOfClass:[NSArray class]]) { 

      tempEndTime = _dataArray[indexPath.section][indexPath.row]; 

  }else {        tempEndTime = _dataArray[indexPath.row];    }   

NSInteger endTime = tempEndTime.longLongValue;  

     return [XinxinTimer getNowTimeWithString:endTime];    }

#pragma mark -  传入结束时间 | 计算与当前时间的差值

+ (NSString *)getNowTimeWithString:(NSInteger )aTimeString{     

  NSDate * sjDate = [NSDate date];   //手机时间   

NSInteger sjInteger = [sjDate timeIntervalSince1970];  // 手机当前时间戳       

NSTimeInterval timeInterval = aTimeString - sjInteger;   

int days = (int)(timeInterval/(3600*24));   

int hours = (int)((timeInterval-days*24*3600)/3600); 

  int minutes = (int)(timeInterval-days*24*3600-hours*3600)/60; 

  int seconds = timeInterval-days*24*3600-hours*3600-minutes*60;     

  NSString *dayStr;NSString *hoursStr;NSString *minutesStr;NSString *secondsStr;       

dayStr = [NSString stringWithFormat:@"%d",days];                     //天    hoursStr = [NSString stringWithFormat:@"%d",hours];                  //小时   

if(minutes<10)                                                       //分钟       

minutesStr = [NSString stringWithFormat:@"0%d",minutes];   

else       

minutesStr = [NSString stringWithFormat:@"%d",minutes];   

if(seconds < 10)                                                     //秒       

secondsStr = [NSString stringWithFormat:@"0%d", seconds];   

else        secondsStr = [NSString stringWithFormat:@"%d",seconds];   

if (hours<=0&&minutes<=0&&seconds<=0) {        return @"支付时间已过!";    }   

return [NSString stringWithFormat:@"剩余兑换时间: %@:%@:%@", hoursStr,minutesStr,secondsStr];    }

/** *  主动销毁定时器 */

-(void)destoryTimer{ 

  if (_timer) {        dispatch_source_cancel(_timer);        _timer = nil;    }}

-(void)dealloc{    NSLog(@"%s dealloc",object_getClassName(self));}@end

相关文章

  • tableview cell 上播放倒计时

    tableview cell 上播放倒计时 项目中有用到cell上播放倒计时,遇到很多的坑,在这里总结一些以免下次...

  • cell上倒计时

    因为商城项目内有秒杀和限时购的需求,需要每个cell上都有一个倒计时,所以写了一个用一个计时器 控制所有cell的...

  • 倒计时

    ios怎么在cell上添加倒计时 iOS中 简单易懂的秒杀倒计时/倒计时 iOS开发-三种倒计时的写法 iOS实现...

  • Cell上的倒计时显示

    需求是这样的,对UICollectionView中的前两个Cell 加入一个倒计时器的显示,如下图: 此时,简单分...

  • 如何释放Cell中的NSTimer

    问题 Cell中使用了NSTimer做倒计时功能,在Cell的dealloc方法中销毁定时器[self.timer...

  • iOS倒计时秒杀界面 tableview

    根据时间数组和cell id数组 获取每个cell的倒计时时间 并在cell显示时赋值 鉴于真机测试会发生后台ti...

  • iOS之TableViewCell上的倒计时

    电商上很多需求都有倒计时功能。如果每个cell上加个定时器,性能上不好,所以这个问题不太好解决。借鉴了下别人思路写...

  • Cell倒计时

    倒计时 工具tool .h m 核心 .h .m VC 中 使用操作 创建视图 并初始化 数据 每秒都要处理 参考...

  • iOS14遇到的问题

    问题1:cell上按钮点击事件及cell上的手势失效 原因:把button添加到cell上之后,被cell的con...

  • iOS 定时中心及高精度定时

    最近一个做电商类APP的朋友说,限时优惠页面 UITableView 上的 Cell 全部都需要倒计时,然后他的预...

网友评论

      本文标题:cell上倒计时

      本文链接:https://www.haomeiwen.com/subject/mwrlixtx.html