zoukankan      html  css  js  c++  java
  • 使用CADisplayLink写秒表

    使用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 = 1;
            [_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

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

  • 相关阅读:
    django 重建一个表
    近期数据工作的知识点总结(model-dict高级用法)
    搬运django中文网 CentOS7下部署Django项目详细操作步骤(django安装网站有时候打不开,备份用)
    创建ftp免密只读用户(外系统读取csv共享数据)
    某某系统从外部基础库读取数据
    离线安装 django-axes
    django queryset用法总结二
    django queryset用法总结一
    nginx 启动,停止 重启
    安装安全狗失败 ,linux wget的时候不去找目标ip,而是路由到其他ip,原因分析
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4029547.html
Copyright © 2011-2022 走看看