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

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

  • 相关阅读:
    yii 引入文件
    CodeForces 621C Wet Shark and Flowers
    面试题题解
    POJ 2251 Dungeon Master
    HDU 5935 Car(模拟)
    HDU 5938 Four Operations(暴力枚举)
    CodeForces 722C Destroying Array(并查集)
    HDU 5547 Sudoku(dfs)
    HDU 5583 Kingdom of Black and White(模拟)
    HDU 5512 Pagodas(等差数列)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4029547.html
Copyright © 2011-2022 走看看