Skip to content

Instantly share code, notes, and snippets.

@ademar111190
Last active October 13, 2015 06:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ademar111190/4155970 to your computer and use it in GitHub Desktop.
Save ademar111190/4155970 to your computer and use it in GitHub Desktop.
CustomChronometer to ios
// CustomChronometer Copyright (C) 2012 Ademar Alves de Oliveira and jurema.la
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// thanks http://www.apptite.be/blog/ios/sample-ios-application-stopwatch/
#import <UIKit/UIKit.h>
@interface CustomChronometer : UILabel
@property (nonatomic,retain) NSTimer *stopWatchTimer;
@property (nonatomic,retain) NSDate *startDate;
@property (nonatomic,retain) NSString *resetLabel;
@property BOOL running;
- (void) updateTimer;
- (void) start;
- (void) stop;
- (void) reset;
- (BOOL) isRunning;
- (double) getElapsedTime;
@end
// CustomChronometer Copyright (C) 2012 Ademar Alves de Oliveira and jurema.la
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "CustomChronometer.h"
@implementation CustomChronometer
@synthesize stopWatchTimer;
@synthesize startDate;
@synthesize resetLabel;
@synthesize running;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
resetLabel = @"00:00";
[self setBackgroundColor:[UIColor clearColor]];
[self reset];
[self setRunning:NO];
}
return self;
}
- (BOOL) isRunning
{
return self.running;
}
- (void) stop
{
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
[self setRunning:NO];
}
- (void) reset
{
startDate = nil;
#if __has_feature(objc_arc)
startDate = [NSDate date];
#else
startDate = [[NSDate date] retain];
#endif
[self setText:resetLabel];
}
- (void) start
{
[self reset];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
[self setRunning:YES];
}
- (void) updateTimer
{
#if __has_feature(objc_arc)
NSDate *currentDate = [NSDate date];
#else
NSDate *currentDate = [[NSDate date] retain];
#endif
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
#if __has_feature(objc_arc)
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
#else
NSDate *timerDate = [[NSDate dateWithTimeIntervalSince1970:timeInterval] retain];
#endif
NSDateFormatter *dateFormatter = [NSDateFormatter new];
//HH:mm:ss.SSS
int x = [[[[NSString stringWithFormat:@"%@",timerDate] componentsSeparatedByString:@" "][1] componentsSeparatedByString:@":"][0] intValue];
if (x > 0)
[dateFormatter setDateFormat:@"HH:mm"];
else
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
[self setText:[dateFormatter stringFromDate:timerDate]];
#if __has_feature(objc_arc)
#else
[dateFormatter release];
[timerDate release];
[currentDate release];
#endif
}
#if __has_feature(objc_arc)
#else
- (void) dealloc
{
[stopWatchTimer release];
[startDate release];
[resetLabel release];
[super dealloc];
}
#endif
- (double) getElapsedTime
{
return [[NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSinceDate:startDate]] timeIntervalSince1970] / 3600;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment