• 使用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

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

  • 相关阅读:
    将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片
    <bean:wirte>的用法
    bean标签库Struts标签库
    <bean:write/>标签的format属性
    ByteArrayOutputStream和ByteArrayInputStream详解
    <bean:write>的用法
    FLASH寿命测试!
    波特率,kbps,Mbps含义
    printf()函数是格式化输出!
    Qt学习Qt中的C++
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4029547.html
Copyright © 2020-2023  润新知