使用CADisplayLink写秒表

效果:

源码:

StopWatch.h 与 StopWatch.m

//
// StopWatch.h
// ShowTime
//
// Created by YouXianMing on 14-10-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @protocol StopWatchDelegate <NSObject>
- (void)stopWatchDate:(NSDate *)date;
@end // 说明:此秒表类是基于CADisplayLink所写,每一帧刷新一次
@interface StopWatch : NSObject @property (nonatomic, assign) id<StopWatchDelegate> delegate; - (void)start; // 开始
- (void)stop; // 停止
- (void)reset; // 复位
- (NSDate *)gainDate; // 获取时间(只有在start的时候获取时间才有意义,stop之后返回值为0) @end
//
// StopWatch.m
// ShowTime
//
// Created by YouXianMing on 14-10-16.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "StopWatch.h" @interface StopWatch () @property (nonatomic, strong) CADisplayLink *countDownTimer;
@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *pausedDate; @property (nonatomic, assign) BOOL startFlag; @end @implementation StopWatch - (void)start {
_startFlag = YES; if (_countDownTimer) {
[_countDownTimer invalidate];
_countDownTimer = nil;
} if (_countDownTimer == nil) {
if(_startDate == nil) {
_startDate = [NSDate date];
} if(_pausedDate != nil){
NSTimeInterval countedTime = [_pausedDate timeIntervalSinceDate:_startDate];
_startDate = [[NSDate date] dateByAddingTimeInterval:-countedTime];
_pausedDate = nil;
} _countDownTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(timerRunEvent)];
_countDownTimer.frameInterval = ;
[_countDownTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
} - (void)stop {
_startFlag = NO;
if (_countDownTimer) {
[_countDownTimer invalidate];
_countDownTimer = nil;
_pausedDate = [NSDate date];
}
} - (void)reset {
_pausedDate = nil;
_startDate = [NSDate date]; if (_delegate) {
NSTimeInterval currentToSpecifyDuration = [[[NSDate alloc] init] timeIntervalSinceDate:_startDate];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentToSpecifyDuration];
[_delegate stopWatchDate:date];
}
} - (NSDate *)gainDate {
if (_startFlag) {
NSTimeInterval currentToSpecifyDuration = [[[NSDate alloc] init] timeIntervalSinceDate:_startDate];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentToSpecifyDuration];
return date;
} else {
return nil;
}
} - (void)timerRunEvent {
if (_delegate) {
NSTimeInterval currentToSpecifyDuration = [[[NSDate alloc] init] timeIntervalSinceDate:_startDate];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentToSpecifyDuration];
[_delegate stopWatchDate:date];
}
} - (void)dealloc {
NSLog(@"xxxxxx");
} @end

注意:富文本显示的秒表并不是这个类的功能而已:)

使用CADisplayLink写秒表的更多相关文章

  1. JavaScript写秒表

    1.HTML部分 <div id="div1"> <span id="hour">00</span> <span> ...

  2. 利用纯代码写出一个秒表表盘的方法 —— #DF

    @interface ViewController () @property (nonatomic, strong) CALayer *secLayer; // 秒针layer @property ( ...

  3. CADisplayLink以及和NSTimer的区别

    什么是CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个r ...

  4. iOS开发中深入理解CADisplayLink和NSTimer

    一.什么是CADisplayLink 简单地说,它就是一个定时器,每隔几毫秒刷新一次屏幕. CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一 ...

  5. Core Animation系列之CADisplayLink

    一直以来都想好好学习下CoreAnimation,奈何涉及的东西太多,想要一次性全部搞定时间上不允许,以后会断断续续的补全.最近项目里用到了CADisplayLink,就顺便花点时间看了看. 一.简介 ...

  6. C# 一个简单的秒表引发的窗体卡死问题

    一个秒表程序也是我的一个心病,因为一直想写这样的一个东西,但是总往GUI那边想,所以就比较怵,可能是上学的时候学MFC搞出的后遗症吧,不过当我今天想好用Win Form(话说还是第一次写win for ...

  7. CADisplayLink

    什么是CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个r ...

  8. Android(java)学习笔记134:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  9. Core Animation系列之CADisplayLink(转)

    转自 http://www.tuicool.com/articles/meMVR3 一直以来都想好好学习下CoreAnimation,奈何涉及的东西太多,想要一次性全部搞定时间上不允许,以后会断断续续 ...

随机推荐

  1. PHP 运行相关概念

    web server.cgi.cgi程序.fast-cgi.php-fpm.php-cgi

  2. 面试题28:单链表一次遍历删除从后往前的第n个节点

    class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode* fake = ); fak ...

  3. BATJ面试必会之 Spring 篇(一)

    译者:深海 校对:方腾飞 出自并发编程网 – ifeve.com 目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP ...

  4. 【转】JavaScript代码性能优化总结

    本文作者:zifan 来自:携程设计委员会 链接:http://ued.ctrip.com/blog/javascript-code-performance-optimization-summary. ...

  5. [转]ORA-28001: the password has expired解决方法

    本文转自:http://blog.csdn.net/btt2013/article/details/54862420 参考文献:http://www.zhetao.com/content259 后台报 ...

  6. Ionic3与Angular4新特性

    之前(17年3月底)Angular4.0.0正式发布,这个月(4月十几号)Ionic3又发布了,很多人看到这个估计都是一脸懵圈,其实,Angular4只是Angular2的后续版本,Ionic3也是I ...

  7. HDU 1428

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Java 源程序与编译型运行区别

  9. Centos7宽带连接

    网上大多教程都是虚拟机上实现宽带连接的,但是实际有差别 装完系统最头疼的就是宽带连接 转自:centos创建宽带连接-EnchanterBlue-ChinaUnix博客 http://blog.chi ...

  10. MySQL免安装版下载与配置

    1.     下载Mysql 官方:http://www.mysql.com→downloads→选社区版本MySQL Community Edition(GPL)→点击Community(GPL)D ...