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

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

  • 相关阅读:
    3D Bezier Curve, Matlab Code
    Matlab Bezier
    python与matlab混合编程
    Matlab P文件——加快Matlab程序,保护你的算法(z)
    Simens NX (原UG)内部代码逆向分析 / Inner code Reverse analysis of NX software
    Matlab扩展编程
    《数值分析及其MATLAB实现》(任玉杰)扫描版[PDF]
    NURBS Toolbox by D.M. Spink (matlab)
    贝塞尔曲线
    BSpline & NURBS (Matlab Code)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4029547.html
Copyright © 2011-2022 走看看