Created
September 3, 2013 22:56
-
-
Save vibrazy/6430712 to your computer and use it in GitHub Desktop.
CBTimer is an simple Objective C timer object for count downs. You can set a limit and start Ascending or Descending.
Using blocks for Callbacks and isFinished boolean.
Using CADisplayLink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do any additional setup after loading the view. | |
[[CBTimer instance] startTimerWithMaxTime:5 mode:CBTimerModeDecrease timerDoneBlock:^ (CFTimeInterval elapsed, BOOL isFinished) | |
{ | |
self.counterLabel.text = [NSString stringWithFormat:@"%.3f",elapsed]; | |
if (isFinished) | |
{ | |
UIAlertView *finished =[[UIAlertView alloc] initWithTitle:@"Finished" message:@"Finito" delegate:nil cancelButtonTitle:@"OKAY" otherButtonTitles:nil]; | |
[finished show]; | |
} | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CBTimer.h | |
// CBColourBlind | |
// | |
// Created by Daniel Tavares on 03/09/2013. | |
// Copyright (c) 2013 Daniel Tavares. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <QuartzCore/QuartzCore.h> | |
typedef void (^CBTimerTickedBlock)(CFTimeInterval elapsed, BOOL isFinished); | |
typedef enum | |
{ | |
CBTimerModeDecrease, | |
CBTimerModeIncrease | |
} CBTimerMode; | |
@interface CBTimer : NSObject | |
{ | |
CADisplayLink *displayLink; | |
CBTimerMode mode; | |
} | |
@property (nonatomic, copy) CBTimerTickedBlock doneBlock; | |
+ (id)instance; | |
-(void)startTimerWithMaxTime:(double)maxTime | |
mode:(CBTimerMode)mode | |
timerDoneBlock:(CBTimerTickedBlock)doneBlock; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CBTimer.m | |
// CBColourBlind | |
// | |
// Created by Daniel Tavares on 03/09/2013. | |
// Copyright (c) 2013 Daniel Tavares. All rights reserved. | |
// | |
#import "CBTimer.h" | |
static BOOL running; | |
@implementation CBTimer | |
{ | |
CFTimeInterval startTimer; | |
CFTimeInterval endTimer; | |
} | |
+ (id)instance | |
{ | |
static dispatch_once_t pred; | |
static CBTimer *instance = nil; | |
dispatch_once(&pred, ^{ instance = [[self alloc] init]; }); | |
return instance; | |
} | |
-(void)startTimerWithMaxTime:(double)maxTime | |
mode:(CBTimerMode)timerMode | |
timerDoneBlock:(CBTimerTickedBlock)doneBlock | |
{ | |
self.doneBlock = doneBlock; //set block | |
startTimer = CFAbsoluteTimeGetCurrent(); //get current time | |
endTimer = (CFTimeInterval)maxTime; //set max time | |
mode = timerMode; | |
[displayLink invalidate]; //stop timer just in case | |
running = NO; | |
if(!running) | |
{ | |
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(startCountDown)]; | |
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; | |
running = YES; | |
} | |
} | |
-(void)startCountDown | |
{ | |
if (running) | |
{ | |
CFTimeInterval diff = 0.0; | |
BOOL isFinished = NO; | |
if (mode==CBTimerModeDecrease) | |
{ | |
diff = endTimer - (CFAbsoluteTimeGetCurrent() - startTimer); | |
if (diff<=0) | |
{ | |
[displayLink invalidate]; | |
running = NO; | |
diff = 0; | |
isFinished = YES; | |
} | |
} | |
else | |
{ | |
diff = (CFAbsoluteTimeGetCurrent() - startTimer); | |
if (diff>=endTimer) | |
{ | |
[displayLink invalidate]; | |
running = NO; | |
isFinished = YES; | |
diff = endTimer; | |
} | |
} | |
//dont let it go below it | |
if (self.doneBlock) | |
{ | |
self.doneBlock(diff,isFinished); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment