All Projects → herobin22 → Oycountdownmanager

herobin22 / Oycountdownmanager

Licence: mit
在cell中使用倒计时的处理方法, 全局使用一个NSTimer对象, 支持单列表.多列表.多页面.分页列表使用

Projects that are alternatives of or similar to Oycountdownmanager

timeleft
⏳ Don't waste your time or time will waste you! ☠️ One tends to consume time on low-return stuff, superficial entertainment. 📺 Things they don't actually want to do. One should make the most of his time. ⏱ Get busy living.
Stars: ✭ 18 (-94.32%)
Mutual labels:  time, countdown
Wrcellview
自定义View,类似tableView的系统cell,使用方便 Custom View, similar to the tableView system cell, easy to use
Stars: ✭ 64 (-79.81%)
Mutual labels:  tableview, cell
Ynexpandablecell
✨ Awesome expandable, collapsible tableview cell for iOS written in Swift 4
Stars: ✭ 445 (+40.38%)
Mutual labels:  tableview, cell
React Native Cell Components
Awesome react-native cell components! From a Cell to more complex & awesome components.
Stars: ✭ 177 (-44.16%)
Mutual labels:  tableview, cell
Use Timer
A timer hook for React
Stars: ✭ 113 (-64.35%)
Mutual labels:  time, countdown
Aiforms.settingsview
SettingsView for Xamarin.Forms
Stars: ✭ 274 (-13.56%)
Mutual labels:  tableview, cell
Jhform
JhForm - 自定义表单工具,更加简单,快捷的创建表单、设置页面
Stars: ✭ 108 (-65.93%)
Mutual labels:  tableview, cell
React Timer Hook
React timer hook
Stars: ✭ 118 (-62.78%)
Mutual labels:  time, countdown
tcscustomrowactionfactory
TCSTableViewRowActionFactory allows you to setup the swipe actions for cells in a table view using UIView and some other convenient methods
Stars: ✭ 24 (-92.43%)
Mutual labels:  tableview, cell
Mspeekcollectionviewdelegateimplementation
A custom paging behavior that peeks the previous and next items in a collection view
Stars: ✭ 265 (-16.4%)
Mutual labels:  cell
Awesome Falsehood
😱 Falsehoods Programmers Believe in
Stars: ✭ 16,614 (+5141.01%)
Mutual labels:  time
Differencekit
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
Stars: ✭ 2,986 (+841.96%)
Mutual labels:  tableview
Countdownview
Android Countdown View
Stars: ✭ 2,869 (+805.05%)
Mutual labels:  countdown
Timecop
A gem providing "time travel", "time freezing", and "time acceleration" capabilities, making it simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
Stars: ✭ 3,110 (+881.07%)
Mutual labels:  time
Javascript Time Ago
International highly customizable relative date/time formatting
Stars: ✭ 263 (-17.03%)
Mutual labels:  time
Vimpyter
Edit your Jupyter notebooks in Vim/Neovim
Stars: ✭ 308 (-2.84%)
Mutual labels:  cell
Shsegmentedcontroltableview
Both scroll horizontal and vertical for segment scrollview which have a same header. — 类似半糖、美丽说主页与QQ音乐歌曲列表布局效果,实现不同菜单的左右滑动切换,同时支持类似tableview的顶部工具栏悬停(既可以左右滑动,又可以上下滑动)。兼容下拉刷新,上拉加载更多。现已加入swift豪华套餐,使用样例助你快速使用
Stars: ✭ 259 (-18.3%)
Mutual labels:  tableview
Tableview
TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells.
Stars: ✭ 2,928 (+823.66%)
Mutual labels:  tableview
M
Stars: ✭ 313 (-1.26%)
Mutual labels:  time
Timestamp
⏰ A better macOS menu bar clock.
Stars: ✭ 296 (-6.62%)
Mutual labels:  time

一.OYCountDownManager描述

iOS在cell中使用倒计时的处理方法, 全局使用一个NSTimer对象

Swift版本: OYCountDownManager-Swift

  • 单个列表倒计时
  • 多个列表倒计时
  • 多个页面倒计时
  • 分页列表倒计时
  • 后台模式倒计时

单个列表多个列表.gif多个页面.gif分页列表.gif

二.原理分析

原理分析图.png

三.使用方法

1.1 第一种方法: 使用cocoapods自动安装

pod 'OYCountDownManager'

1.2 第二种方法

下载示例Demo, 把里面的OYCountDownManager文件夹拖到你的项目中

2. 在第一次使用的地方调用[kCountDownManager start]

- (void)viewDidLoad {
    [super viewDidLoad];

    // 启动倒计时管理
    [kCountDownManager start];
}

3. 在Cell初始化中监听通知 kCountDownNotification

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    }
    return self;
}

4. 在cell设置通知回调, 取得时间差, 根据时间差进行处理

- (void)countDownNotification {
    /// 计算倒计时
    NSInteger countDown = [self.model.count integerValue] - kCountDownManager.timeInterval;
    if (countDown <= 0) {
          // 倒计时结束时回调
          xxxx(使用代理或block)
          return;
    }
    /// 重新赋值
    self.timeLabel.text = [NSString stringWithFormat:@"倒计时%02zd:%02zd:%02zd", countDown/3600,       (countDown/60)%60, countDown%60];
}

5. 当刷新数据时,调用reload方法

- (void)reloadData {
    // 网络加载数据

    // 调用[kCountDownManager reload]
    [kCountDownManager reload];
    // 刷新
    [self.tableView reloadData];
}

6. 当不需要倒计时时, 废除定时器

[kCountDownManager invalidate];

四.高级使用(多列表.多页面.分页列表)

增加identifier:标识符, 一个identifier支持一个倒计时源, 有一个单独的时间差

/** 添加倒计时源 */
- (void)addSourceWithIdentifier:(NSString *)identifier;

/** 获取时间差 */
- (NSInteger)timeIntervalWithIdentifier:(NSString *)identifier;

/** 刷新倒计时源 */
- (void)reloadSourceWithIdentifier:(NSString *)identifier;

/** 刷新所有倒计时源 */
- (void)reloadAllSource;

/** 清除倒计时源 */
- (void)removeSourceWithIdentifier:(NSString *)identifier;

/** 清除所有倒计时源 */
- (void)removeAllSource;

以一个页面有两个独立的列表为例

1.定义identifier(常量)

NSString *const OYMultipleTableSource1 = @"OYMultipleTableSource1";
NSString *const OYMultipleTableSource2 = @"OYMultipleTableSource2";

2.增加倒计时源

// 增加倒计时源 
[kCountDownManager addSourceWithIdentifier:OYMultipleTableSource1];
[kCountDownManager addSourceWithIdentifier:OYMultipleTableSource2];

3.在cell通知回调中, 通过identifier取得时间差, 根据时间差进行处理

- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    /// 计算倒计时
    OYModel *model = self.model;
    /// 根据identifier取得时间差, 以OYMultipleTableSource1为例
    NSInteger timeInterval = timeInterval = [kCountDownManager timeIntervalWithIdentifier: OYMultipleTableSource1];
    }
    NSInteger countDown = model.count - timeInterval;
    /// 当倒计时到了进行回调
    if (countDown <= 0) {
        self.detailTextLabel.text = @"活动开始";
        // 倒计时结束时回调
          xxxx(使用代理或block)
        return;
    }
    /// 重新赋值
    self.detailTextLabel.text = [NSString stringWithFormat:@"倒计时%02zd:%02zd:%02zd", countDown/3600, (countDown/60)%60, countDown%60];
}

4. 当刷新数据时,调用reloadSourceWithIdentifier:刷新时间差

- (void)reloadData {
    // 网络加载数据

   // 调用reloadSourceWithIdentifier:刷新时间差
   [kCountDownManager reloadSourceWithIdentifier:OYMultiplePageSource1];
    // 刷新
    [self.tableView reloadData];
}

5. 当页面销毁, 移除倒计时源, 或者不需要定时器, 废除定时器

// 移除所有倒计时源
[kCountDownManager removeAllSource];
// 废除定时器
[kCountDownManager invalidate];

五.注意事项

误差分析

  • NSTimer可以精确到50-100毫秒,不是绝对准确的,所以你使用时间累加的方法时间久了有可能成为时间误差的来源
  • 以秒为单位触发定时器, 当reloadData后, 定时器也许刚好到达触发点, 时间差+1, 数据刚reload完就马上-1秒
  • 后台模式是以进入后台的绝对时间, 及进入前台的绝对时间做差值来计算的, 差值会进行取整, 导致一点点误差

滚动cell时出去文字闪烁

在给cell的模型赋值后, 最好手动调用一下countDownNotification方法, 保证及时刷新

///  重写setter方法
- (void)setModel:(Model *)model {
    _model = model;
    self.titleLabel.text = model.title;
    // 手动调用通知的回调
    [self countDownNotification];
}

倒计时为0后出现复用问题

在倒计时为0后, 应该回调给控制器, 从后台请求一次数据, 保证倒计时没有出现误差

if (countDown <= 0) {
          // 倒计时结束时回调
          xxxx(使用代理或block)
    }return;

出现每秒倒计时减2的问题

1.查看定时器设置是否正确, 或者通知是否监听了两次

2.在countDownNotification方法中, 是否用[NSDate date]做了某些计算, 因为[NSDate date]为当前时间, 每一秒去取都会比上一秒大一秒, 再加上timeInterval也是一秒加一, 那么就会出现每秒倒计时减2的问题

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].