使用CADisplayLink写秒表

使用CADisplayLink写秒表

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

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

 

你可能感兴趣的:(display)