基于dispatch_after封装YXTimer

本人根据dispatch_after封装了一个定时器,支持block以及代理的方式来激活定时器,适用于对精度要求低,耗时短的地方,高端大气上档次,低调奢华有内涵:)

源码:

YXTimer.h 与 YXTimer.m

//
// YXTimer.h
// YXTimer
//
// Created by YouXianMing on 14-10-2.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
@class YXTimer; @protocol YXTimerDelegete <NSObject>
- (void)YXTimerEvent:(YXTimer *)timer;
@end @interface YXTimer : NSObject // 代理相关方法
@property (nonatomic) NSTimeInterval milliSecond; // 毫秒
@property (nonatomic, assign) id<YXTimerDelegete> delegate; // block相关方法
+ (instancetype)timerWithMilliSecondInterval:(NSTimeInterval)milliSecond
Block:(void (^)(YXTimer *timer))block; // 激活定时器 + 停止定时器
- (void)start;
- (void)stop; // 注意:要想让timer能被移除,一定要激活stop方法才行 @end
//
// YXTimer.m
// YXTimer
//
// Created by YouXianMing on 14-10-2.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #ifdef DEBUG
#define YXTimer_DLog(fmt, ...) NSLog((@"YXTimer.m:%s:%d" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define YXTimer_DLog(...)
#endif #import "YXTimer.h" @interface YXTimer () @property (nonatomic) BOOL isRunning;
@property (nonatomic, copy) void (^block)(YXTimer *timer); @end @implementation YXTimer - (instancetype)init
{
self = [super init];
if (self) {
_isRunning = NO;
}
return self;
} - (instancetype)initWithBlock:(void (^)(YXTimer *timer))block {
self = [super init]; if (self) {
_isRunning = NO;
_block = block;
} return self;
} - (void)start {
_isRunning = YES;
[self runTimer];
} - (void)stop {
_isRunning = NO;
} + (instancetype)timerWithMilliSecondInterval:(NSTimeInterval)milliSecond
Block:(void (^)(YXTimer *timer))block {
YXTimer *timer = [[YXTimer alloc] initWithBlock:block];
timer.milliSecond = milliSecond; return timer;
} - (void)runTimer { if (self.isRunning) { NSTimeInterval milliSecond = .f;
if (_milliSecond > ) {
milliSecond = _milliSecond;
} dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(milliSecond * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
if (self.block) {
self.block(self);
} if (_delegate) {
[_delegate YXTimerEvent:self];
} [self runTimer];
});
}
} - (void)dealloc {
[self stop];
YXTimer_DLog(@"资源释放了");
} @end

使用源码:

//
// ViewController.m
// YXTimer
//
// Created by YouXianMing on 14-10-2.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "YXTimer.h" @interface ViewController ()<YXTimerDelegete> @property (nonatomic, strong) YXTimer *timer;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic) NSInteger count; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _dataArray = @[@"Y.X. Loading .",
@"Y.X. Loading ..",
@"Y.X. Loading ...",
@"Y.X. Loading ....",
@"Y.X. Loading .....",
@"Y.X. Loading ......",
@"Y.X. Loading ......."]; _timer = [YXTimer new];
_timer.milliSecond = ;
_timer.delegate = self;
[_timer start]; _label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_label.textAlignment = NSTextAlignmentLeft;
_label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
_label.textColor = [UIColor redColor];
_label.center = self.view.center;
[self.view addSubview:_label]; } - (void)YXTimerEvent:(YXTimer *)timer
{
_label.text = _dataArray[_count++ % _dataArray.count];
} @end

需要注意的地方:

基于dispatch_after封装YXTimer的更多相关文章

  1. 基于highcharts封装的组件-demo&源码

    前段时间做的项目中需要用到highcharts绘制各种图表,其实绘制图表本身代码很简单,但是由于需求很多,有大量的图形需要绘制,所以就不得不复制粘贴大量重复(默认配置等等)的代码,所以,后来抽空自己基 ...

  2. 基于jQuery封装的分页组件

    前言: 由于项目需要实现分页效果,上jQuery插件库找了下,但是木有找到自己想要的效果,于是自己封装了个分页组件. 思路: 主要是初始化时基于原型建立的分页模板然后绑定动态事件并实现刷新DOM的分页 ...

  3. 基于AFN封装的带缓存的网络请求

    给大家分享一个基于AFN封装的网络请求 git: https://github.com/zhouxihi/NVNetworking #带缓存机制的网络请求 各类请求有分带缓存 , 不带缓存, 可自定义 ...

  4. 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件

    目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...

  5. 基于epoll封装的事件回调miniserver

    epoll技术前两节已经阐述过了,目前主要做一下封装,很多epoll的服务器都是采用事件回调方式处理, 其实并没有什么复杂的,我慢慢给大家阐述下原理. 在networking.h和networking ...

  6. vue可视化图表 基于Echarts封装好的v-charts简介

    **vue可视化图表 基于Echarts封装好的v-charts** 近期公司又一个新的需求,要做一个订单和销售额统计的项目,需要用到可视化图表来更直观的展示数据.首先我想到的是Echarts,众所周 ...

  7. EasyRTSPClient:基于live555封装的支持重连的RTSP客户端RTSPClient

    今天先简单介绍一下EasyRTSPClient,后面的文章我们再仔细介绍EasyRTSPClient内部的设计过程: EasyRTSPClient:https://github.com/EasyDar ...

  8. 基于better-scroll封装一个上拉加载下拉刷新组件

    1.起因 上拉加载和下拉刷新在移动端项目中是很常见的需求,遂自己便基于better-scroll封装了一个下拉刷新上拉加载组件. 2.过程 better-scroll是目前比较好用的开源滚动库,提供很 ...

  9. Python3操作MySQL基于PyMySQL封装的类

    Python3操作MySQL基于PyMySQL封装的类   在未使用操作数据库的框架开发项目的时候,我们需要自己处理数据库连接问题,今天在做一个Python的演示项目,写一个操作MySQL数据库的类, ...

随机推荐

  1. Etcd安全配置之Basic Auth认证

    <中小团队落地配置中心详解>文章中我们介绍了如何基于Etcd+Confd构建配置中心,最后提到Etcd的安全问题时说了可以使用账号密码认证以达到安全访问的目的,究竟该如何开启认证以及怎么设 ...

  2. Java正则表达式防注入小例子

    /** * 第一行匹配特殊字符: * 第二行匹配开头和结尾都有空格的: * 第三行匹配结尾有空格的 * */ private Boolean DetermineChar(String reqData, ...

  3. CUBA 7 新特性(上篇)

    三年前,我们宣布了 CUBA 框架的第二个公开的主版本.CUBA 6 是改变游戏规则的版本 - 框架的许可从私有化变成了公开的 Apache2.0.那些日子里,我们甚至猜不到这个变化会最终将框架带向何 ...

  4. 合天misc100

    打开txt文件是一串RGB颜色值 用len(file.readlines()),发现颜色值有61366个,能分解成122*503 from PIL import Image length = 122 ...

  5. C学习笔记(2)--指针

    一.多文件结构总结 1.子源文件里面包含自己对应的头文件 2.无论是何源文件调用库函数,都需要包含该库函数的声明所在的头文件 3.头文件又叫接口文件,.c对数据和函数进行封装和包含, .h就是.c对外 ...

  6. Differences between write through and write back

    https://stackoverflow.com/questions/27087912/write-back-vs-write-through

  7. 深入理解MyBatis的原理(一): 独立的入门demo

    前言:不结合spring,只有 mybatis+maven.数据库使用 oracle.不尝试永远不知道会发生什么事,其中遇到两个小问题,也记录下来了.转载请注明出处:https://www.cnblo ...

  8. Go 语言实现 HTTP 层面的反向代理

    最近对 Go 语言的反向代理使用得偏多,其实在大概两年前就写过 TCP 层面的代理,而且那时也是用的 Go 语言,不同之处在于之前只是偶尔尝试一下使用,最近是因为工作需要使用的.相比较于 TCP 层面 ...

  9. macbook usb口突然不能用 解决方法

    1.先按shift+ctrl+opt+开机键,等待10秒,着10秒是没有反应的,屏幕不会亮,系统不会跑起来.2.10秒过后松开所以的键.3.再按opt+cmd+r+p接着按开机键,这时电脑会不断重启, ...

  10. 小tip: CSS后代选择器可能的错误认识——张鑫旭

    一.关于类选择器的一个问题 假设有下面一个面试题,CSS代码如下: .red { color: red; } .green { color: green; } HTML如下: <div clas ...