利用kvo实现列表倒计时
自己稍微记录一下,方便以后用到:
先创建一个定时器的类:
#import "TimeCenter.h"
@interface TimeCenter ()
@property (nonatomic, assign) NSInteger timeInterval;// 累计时间
@property (nonatomic, strong) NSTimer *timer; @end
@implementation TimeCenter
+ (instancetype)shareCenter {
static TimeCenter *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[TimeCenter alloc]init];
}); return center;
}
//启动
- (void)start {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];
self.timer=timer;
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
//停止
- (void)stop
{
if (_timer != nil) {
[self.timer invalidate];
}
} //事件
- (void)timerEvent
{
if (_timer != nil) {
self.timeInterval++;
}
}
在控制器创建的时候开始定时器,
然后在cell中添加kvo 监听事件:
@implementation TableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *ID = @"tableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
} - (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//第二种:KVO监听
[[TimeCenter shareCenter] addObserver:self
forKeyPath:@"timeInterval"
options:NSKeyValueObservingOptionOld
|NSKeyValueObservingOptionNew
context:nil];
}
return self;
} - (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
if ([keyPath isEqual:@"timeInterval"]) {
[self reloadCell];
}
} - (void)setTimeModel:(TimeModel *)timeModel{
_timeModel=timeModel;
[self reloadCell];
} - (void)reloadCell{
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
NSInteger time = [date timeIntervalSince1970];
NSInteger leftTime = [self.timeModel.time integerValue] - time;
if (leftTime <0){
self.textLabel.text =@"已结束";
}else{
self.textLabel.text = [NSString stringWithFormat:@"剩余%ld天%02ld:%02ld:%02ld", leftTime/(60*60*24),
(leftTime/3600)%24,
(leftTime/60)%60,
leftTime%60];
}
}
- (void)dealloc
{
[[TimeCenter shareCenter] removeObserver:self
forKeyPath:@"timeInterval"];
利用kvo实现列表倒计时的更多相关文章
- Android利用RecyclerView实现列表倒计时效果
最近面试时,面试官问了一个列表倒计时效果如何实现,然后脑袋突然懵的了O(∩_∩)O,现在记录一下. 运行效果图 实现思路 实现方法主要有两个: 1.为每个开始倒计时的item启动一个定时器,再做更新i ...
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- 利用echo命令实现倒计时的功能
echo -e:启用反斜线控制字符的转换 -E:关闭反斜线控制字符的转换(预设如此) -n:取消行末之换行符号(与 -e 选项下的 \c 字符同意 -e参数下的控制参数 \ ...
- java翻译到mono C#实现系列(4) 利用CountDownTimer类实现倒计时功能 mono版
群里的朋友问利用CountDownTimer类实现倒计时功能怎么实现,我就百度了,参考http://blog.csdn.net/qq344429461/article/details/7521361写 ...
- 利用kvo对集合进行操作
利用kvo对集合进行操作 NSLog(@"其他学生的成绩%@", [array valueForKeyPath:@"point"]); NSLog(@" ...
- python3之利用字典和列表实现城市多级菜单
利用字典和列表实现城市多级菜单 #coding:utf-8 #利用字典和列表实现城市多级菜单 addrIndex = {":"福建"} addrDict = {" ...
- C利用可变参数列表统计一组数的平均值,利用函数形式参数栈原理实现指针运算
//描述:利用可变参数列表统计一组数的平均值 #include <stdarg.h> #include <stdio.h> float average(int num, ... ...
- 微信小程序 列表倒计时
最近要实现一个列表倒计时的功能,写了个demo 展示图 <view class="center colu"> <view class="time&quo ...
- 利用UICollectionView实现列表和宫格视图的切换
很多时候我们需要列表和宫格视图的来回切换,就像苹果的天气应用一样,我之前见过一个用tableview和collectionview来实现这种效果的,我本人不太喜欢这个,那么有没有更好的方法呢?答案是: ...
随机推荐
- 洛谷P1275 魔板
P1275 魔板 题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状 ...
- Java Script 第二章.
对象: JavaScript中的所有事物都是对象:字符串,数组,数值,函数..... JavaScript中提供多个内建对象,比如说 String, Date, Array等等.对象只是带有属性和 ...
- ORACLE 12.1.0.1 至12.1.0.2升级文档(单机版 DBUA方式)
12C DBUA新特性 1. 新的pre-upgrade 检查工具. 2. 并行升级. 3. DBUA升级时,默认并行度为CPU个数或2. 并行度可调整. 4. 在升级过程中,DBUA工具可再次调用( ...
- MySQL服务器与MySQL57服务器区别与不同处在哪里,他们各自的领域范围,能不能同时启动服务?
安装了MySQL-5.7.18.0版本数据库,版本中包含了MySQL Workbench可视化试图工具,在服务列表栏中会有MySQL的两个服务器:如果启动第一项MySQL服务器就只能操作数据库,外界不 ...
- Python之PIP安装
Python有两个著名的包管理工具easy_install.py和pip.Python2.7的安装包中自带了easy_install.py,而pip需要手动安装.而在Python3.5之后都是默认安装 ...
- Codeforces Round #365 (Div. 2) B
Description Little Mishka is a great traveller and she visited many countries. After thinking about ...
- 解决 This application requires Java Runtime Environment XX
已经安装了 jdk ,并且设置好了 java 环境变量,CMD 运行 java 或 javac 都正常,其他依赖 jdk 的应用程序都能正常运行.但是在运行 jd-gui 1.1.0 的时候,出现错误 ...
- python入门之random模块
#!/usr/bin/env python #_*_encoding: utf-8_*_ import random print(random.random()) #生成一个在0到1之间的随机浮点数 ...
- Windows下搭建QT环境
必须软件 qt-windows-opensource-5.1.1-msvc2010-x86-offline qt-vs-addin-1.2.2-opensource支持vs2008.2010.2012 ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...