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

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

这是一个关于特价秒杀的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

目录
相关文章
倒计时15分钟-兼容ios手机效果demo(整理)
倒计时15分钟-兼容ios手机效果demo(整理)
|
程序员 定位技术 iOS开发
iOS开发:解决App进入后台,倒计时(定时器)不能正常计时的问题
在iOS开发过程中,尤其是发送短信验证码的需求是非常常见的需求,这就涉及到倒计时的使用,但是如果正在倒计时操作,app进入后台运行,倒计时会出现什么效果呢?那么本篇博文就来了解一下相关知识吧。
1354 1
iOS开发:解决App进入后台,倒计时(定时器)不能正常计时的问题
|
缓存 API iOS开发
iOS 轮播图缩放动态图和网图混播
iOS 轮播图缩放动态图和网图混播
iOS 轮播图缩放动态图和网图混播
|
API iOS开发 Perl
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
|
Java 图形学 Android开发
Android/iOS内嵌Unity开发示例
Android/iOS内嵌Unity开发示例
521 0
Android/iOS内嵌Unity开发示例
|
iOS开发
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
166 0
|
iOS开发
iOS开发-三种倒计时的写法
iOS开发-三种倒计时的写法
206 0
|
JavaScript iOS开发
js 实现倒计时功能,兼容ios,类似京东
js 实现倒计时功能,兼容ios,类似京东
193 0
js 实现倒计时功能,兼容ios,类似京东
|
iOS开发
iOS倒计时按钮闪烁问题解决
iOS倒计时按钮闪烁问题解决
152 0