iOS 特价秒杀(轮播图内嵌倒计时)

96
paperclouds
2016.11.22 11:43* 字数 26

这是一个关于特价秒杀的demo

#import "XXXViewController.h"
#import "XXXDetailViewController.h"
#import "SaleHeaderView.h"
#import "SaleScollModel.h"
@implementation XXXViewController
{
    SaleHeaderView *_saleHeaderView;
    NSTimer *_lastTimer;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollArray = [NSMutableArray array];
    self.itemsArray=[NSMutableArray array];
    self.tableViewArray=[NSMutableArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:style];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadSaleData)];
    _saleHeaderView =[[SaleHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 225)];
    _saleHeaderView.collection.collectionViewDelegate = self;
    [self loadSaleData];
}
- (void)loadSaleData{
    // API为你的接口地址
    [[NSNetworking sharedManager]get:API parameters:nil success:^(id response) {
        SaleScollModel *model = [[SaleScollModel alloc]initWithDictionary:response error:nil];
        
        [self.scrollArray removeAllObjects];
        [self.scrollArray addObjectsFromArray:model.items];
        
        _saleHeaderView.collection.dataArray = self.scrollArray;
        _saleHeaderView.collection.imagesCount = self.scrollArray.count;
        if (self.scrollArray.count != 0) {
            self.tableView.tableHeaderView = _saleHeaderView;
        }else{
            self.tableView.tableHeaderView = nil;
        }
        [self refreshLessTime];        
        [_lastTimer invalidate];
        [self initOverTime];
    } failure:^(NSString *error, int code) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
}
- (void)refreshLessTime
{
    for (int i = 0; i < self.scrollArray.count; i++) {
        SaleModel *model=self.scrollArray[i];
        NSDictionary *dic = @{@"beginDate":model.begin_date,@"endDate": model.end_date};
        NSUInteger overSec = [self getOverTime:dic];
        model.residue_time=[self lessSecondToDay :overSec--];
        NSInteger leaveNum = [model.all_num integerValue]-[model.sale_num integerValue];
        model.percent = [NSString stringWithFormat:@"%f",leaveNum/[model.all_num floatValue]];
    }
     [_saleHeaderView.collection reloadData];
}
-(void)initOverTime
{
    [self startTimer];
}
//开启定时器方法:
- (void)startTimer
{
    _lastTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:nil repeats:YES];
    //    如果不添加下面这条语句,在UITableView拖动的时候,会阻塞定时器的调用
    [[NSRunLoop currentRunLoop] addTimer:_lastTimer forMode:UITrackingRunLoopMode];
}
-(NSUInteger)getOverTime :(NSDictionary *)dataInfo
{
    NSDate *systemDate= [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *beginDate = [formatter dateFromString:[dataInfo objectForKey:@"beginDate"]];
    NSDate *endDate =  [formatter dateFromString:[dataInfo objectForKey:@"endDate"]];
    NSUInteger systemInt=[systemDate timeIntervalSince1970];
    NSUInteger startInt=[beginDate timeIntervalSince1970];
    NSUInteger endInt=[endDate timeIntervalSince1970];
    
    NSInteger residue;
    if (startInt>systemInt){
        residue=startInt-systemInt;  //开始时间大于现在的时间,团购未开始
    }else
    {
        residue=endInt-systemInt;//团购开始,还能多少时间结束
    }
    return residue;
}
- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
    NSUInteger day  = (NSUInteger)seconds/(24*3600);
    NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600+day*24;
    NSUInteger min  = (NSUInteger)(seconds%(3600))/60;
    NSUInteger second = (NSUInteger)(seconds%60);
    
    NSString *time = [NSString stringWithFormat:@"%lu小时%lu分%lu秒",(unsigned long)hour,(unsigned long)min,(unsigned long)second];
    return time;
}
#pragma mark - TTCollectionViewDelegate
// 点击轮播图进入详情页
- (void)cellClickWithIndex:(SaleModel *)model {
    XXXDetailViewController *detail = [[XXXDetailViewController alloc] init];
    detail.desId = model.desId;
    detail.isSale = YES;
    [self.mainVC.navigationController pushViewController:detail animated:YES];
}
#import "SaleHeaderView.h"

@implementation SaleHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
    }
    return self;
}

- (void)addSubViews{
    // TTCollectionView为第三方轮播图
    self.collection = [[TTCollectionView alloc]initWithFrame:self.frame];
    [self addSubview:self.collection];
    // 这里设置轮播图的自动播放时间间隔
    self.collection.timeInterval = 3.0;
}

@end
@implementation TTCollectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SaleModel *model = self.dataArray[indexPath.row];
    CollectionViewCell * cell = [CollectionViewCell dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    NSInteger leaveNum = [model.all_num integerValue]-[model.sale_num integerValue];
    if (leaveNum<0) {
        leaveNum=0;
    }
    cell.leaveNumLbl.text = [NSString stringWithFormat:@"%ld",leaveNum];
    if (leaveNum > 0) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *beginDate = [formatter dateFromString:model.begin_date];
        NSDate *endDate = [formatter dateFromString:model.end_date];
        NSDate *systemDate = [NSDate date];
        NSUInteger systemInt=[systemDate timeIntervalSince1970];
        NSUInteger startInt=[beginDate timeIntervalSince1970];
        NSUInteger endInt=[endDate timeIntervalSince1970];
        
        if (startInt>systemInt) {
            cell.beginOrEndLbl.text = @"距开始";
            cell.timeLbl.hidden = NO;
        }else
        {
            if (endInt < systemInt) {
                cell.beginOrEndLbl.text = @"已结束";
                cell.timeLbl.hidden = YES;
            }else{
            cell.beginOrEndLbl.text = @"距结束";
            cell.timeLbl.hidden = NO;
            }
        }
    }else{
        cell.beginOrEndLbl.text = @"已抢完";
        cell.timeLbl.hidden = YES;
    }
    cell.timeLbl.text=model.residue_time;
    
    return cell; 
}
@end

附:TTCollectionView下载地址:
https://github.com/DreamFlyingCow/TTAutoScrollView

日记本
Web note ad 1