Saturday, 24 August 2013

Countdown Timer using seconds and milliseconds

Countdown Timer using seconds and milliseconds

The goal of my timer is to:
Allow the user to count down from three pre-set times: 15secs, 30 secs, 60
secs The timer will count down to 00:00 and stops. The user can at any
time stop/start time or move countdown to new countdown time.
I have been able to do all of the above except for resetting the countdown
timer when it reaches 00:00. The problem is that when timer hits 00:00 it
no longer correctly starts the timer with the users new choice in time.
(The project is setup over nibs and has references to outlets belonging to
that nib).
@interface TimerController : UIView
@property (strong, nonatomic) IBOutlet UILabel *simplePageTitle;
@property (strong, nonatomic) IBOutlet UILabel *timerText;
- (IBAction)StartStop:(id)sender;
- (IBAction)fifteenSec:(id)sender;
- (IBAction)thirtySec:(id)sender;
- (IBAction)sixtySec:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *startstopButton;
@property int xtime;
@property NSTimer *millisecTimer;
@property int sec;
@property NSTimer *countdownTimer;
@property BOOL startCountdown;
@property BOOL resetTimer;
@property BOOL fifteen;
@property BOOL thirty;
@property BOOL sixty;
- (IBAction)StartStop:(id)sender {
if([sender isSelected]){
// NSLog(@"simple0");
startCountdown = NO;
[sender setSelected:NO];
} else {
// NSLog(@"simple1");
startCountdown = YES;
[sender setSelected:YES];
}
}
TimerController Implementation
- (IBAction)fifteenSec:(id)sender {
xtime = 1500;
fifteen = YES;
resetTimer = YES;
}
- (IBAction)thirtySec:(id)sender {
xtime = 3000;
thirty = YES;
resetTimer = YES;
}
- (IBAction)sixtySec:(id)sender {
xtime = 6000;
sixty = YES;
resetTimer = YES;
}
Main View controller
-(void)timerRun {
// NSLog(@"Timer Runner");
// IF USER PRESSES STARTCOUNTDOWN DO
if (self.timerPageView.startCountdown) {
if ((seconds >= 0 && milliseconds >= 0) ||
self.timerPageView.resetTimer == YES) {
self.timerPageView.sec = self.timerPageView.sec - 1;
seconds = self.timerPageView.sec / 100;
milliseconds = (self.timerPageView.sec * 10) % 1000;
NSString *timerOutput = [NSString stringWithFormat:@"%i:%.0f",
seconds, milliseconds];
self.timerPageView.timerText.text = timerOutput;
}
else if (((seconds == 0 && milliseconds == 0) &&
self.timerPageView.resetTimer != 0 && self.timerPageView.resetTimer ==
0))
{
NSLog(@"TR1");
NSString *timerOutput = @"0:00";
self.timerPageView.timerText.text = timerOutput;
//self.timerPageView.resetTimer = YES;
self.timerPageView.startCountdown = NO;
}
}
if (self.timerPageView.resetTimer == YES) {
NSLog(@"TR2");
[self setSpeedOfTimer];
}
}
-(void)setTimer
{
// SET TIMER SPACE INTERVALS
NSLog(@"Set Timer");
self.timerPageView.countdownTimer = [NSTimer
scheduledTimerWithTimeInterval:0.01 target:self
selector:@selector(timerRun) userInfo:Nil repeats:YES];
}
-(void)setSpeedOfTimer {
if (self.timerPageView.xtime == 0) {
self.timerPageView.xtime = 6000;
self.timerPageView.sec = self.timerPageView.xtime;
self.timerPageView.resetTimer = NO;
NSString *timerOutput = [NSString stringWithFormat:@"%i:%.0f",
seconds, milliseconds];
self.timerPageView.timerText.text = timerOutput;
NSLog(@"Passed1");
}
else if (self.timerPageView.resetTimer == YES) {
self.timerPageView.sec = self.timerPageView.xtime;
self.timerPageView.resetTimer = NO;
// NSString *timerOutput = [NSString stringWithFormat:@"%i:%.0f",
seconds, milliseconds];
// self.timerPageView.timerText.text = timerOutput;
NSLog(@"Passed2");
}
else {
NSString *timerOutput = @"00:00";
self.timerPageView.sec = self.timerPageView.xtime;
self.timerPageView.timerText.text = timerOutput;
NSLog(@"Passed3");
self.timerPageView.resetTimer = NO;
//self.timerPageView.startCountdown = NO;
}
if (self.timerPageView.sixty == YES) {
self.timerPageView.sec = self.timerPageView.xtime;
NSString *timerOutput = @"60:00";
self.timerPageView.timerText.text = timerOutput;
self.timerPageView.sixty = NO;
self.timerPageView.startCountdown = NO;
self.timerPageView.fifteen = NO;
self.timerPageView.thirty = NO;
NSLog(@"Passed6");
}
else if (self.timerPageView.fifteen == YES) {
NSString *timerOutput = @"15:00";
self.timerPageView.timerText.text = timerOutput;
// self.timerPageView.fifteen = NO;
self.timerPageView.startCountdown = YES;
self.timerPageView.sixty = NO;
self.timerPageView.thirty = NO;
self.timerPageView.fifteen = YES;
NSLog(@"%i", self.timerPageView.fifteen);
NSLog(@"Passed4");
}
else if (self.timerPageView.thirty == YES) {
self.timerPageView.sec = self.timerPageView.xtime;
NSString *timerOutput = @"30:00";
self.timerPageView.timerText.text = timerOutput;
// self.timerPageView.thirty = NO;
self.timerPageView.startCountdown = NO;
self.timerPageView.sixty = NO;
self.timerPageView.fifteen = NO;
NSLog(@"Passed5");
}
}
Any help would be great.

No comments:

Post a Comment