Pages

Subscribe:

Friday, July 15, 2011

Countdown Timer with remaining time & custom label (hr:min:sec)

-(IBAction)timerStartButtonPressed
{
if(!isStarted)
{
isStarted = TRUE;
isPaused = FALSE;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFunction:) userInfo:nil repeats:YES];
}
}

-(IBAction)timerPauseButtonPressed
{
if(!isPaused)
{
isStarted = FALSE;
isPaused = TRUE;
[timer invalidate];
}
}



-(void)timerFunction:(NSTimer *)theTimer
{
second = second-1;
remSecond = remSecond + 1;

if(hour >= 0)
{
if (second < 0)
minute = minute-1;

if(remSecond > 59)
remMinute = remMinute + 1;

if(minute < 0)
hour = hour - 1;

if(remMinute > 59)
remHour = remHour + 1;

if(minute < 0 && second < 0 && hour < 0)
{
strSecond = @"00";
strMinute = @"00";
strHour = @"00";

NSString *tempTestDuration = [[NSUserDefaults standardUserDefaults] valueForKey:@"restDuration"];
NSArray *tempArray = [tempTestDuration componentsSeparatedByString:@":"];

remHour = [[tempArray objectAtIndex:0] intValue];
remMinute = [[tempArray objectAtIndex:1] intValue];
remSecond = [[tempArray objectAtIndex:2] intValue];


isStarted = FALSE;
isPaused = TRUE;

[timer invalidate];
timer = nil;

if(isTestRunning && remainingSet < totalSet)
{
[self resetRest];
[self timerStartButtonPressed];
}
else if(!isTestRunning && remainingSet < totalSet)
{
[self resetTest];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"isNewSongInEachTestOn"] == YES)
[appDelegate.myPlayer skipToNextItem];
[self timerStartButtonPressed];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Finish!!!" message:@"Time up." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}

return;
}

if (second>=0)
{
if (second<10)
strSecond = [NSString stringWithFormat:@"0%d", second];
else
strSecond = [NSString stringWithFormat:@"%d", second];
}
else
{
second=59;
strSecond = [NSString stringWithFormat:@"%d", second];
}

if(remSecond <= 59)
{
if (remSecond<10)
strRemSecond = [NSString stringWithFormat:@"0%d", remSecond];
else
strRemSecond = [NSString stringWithFormat:@"%d", remSecond];
}
else
{
remSecond=0;
strRemSecond = [NSString stringWithFormat:@"0%d", remSecond];
}

if(minute >= 0)
{
if (minute<10)
strMinute = [NSString stringWithFormat:@"0%d", minute];
else
strMinute = [NSString stringWithFormat:@"%d", minute];
}
else
{
minute = 59;
strMinute = [NSString stringWithFormat:@"%d",minute];
}

if(remMinute <= 59)
{
if (remMinute<10)
strRemMinute = [NSString stringWithFormat:@"0%d", remMinute];
else
strRemMinute = [NSString stringWithFormat:@"%d", remMinute];
}
else
{
minute = 0;
strMinute = [NSString stringWithFormat:@"0%d",remMinute];
}


if (hour<10)
strHour = [NSString stringWithFormat:@"0%d", hour];
else
strHour= [NSString stringWithFormat:@"%d", hour];

if (remHour<10)
strRemHour = [NSString stringWithFormat:@"0%d", remHour];
else
strRemHour= [NSString stringWithFormat:@"%d", remHour];

NSString *tempStr = [NSString stringWithFormat:@"%@:%@:%@",strHour,strMinute,strSecond];
NSString *tempStrRem = [NSString stringWithFormat:@"%@:%@:%@",strRemHour,strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
strHour = [NSString stringWithFormat:@"%c",[tempStr characterAtIndex:1]];
tempStr = [NSString stringWithFormat:@"%@:%@:%@",strHour,strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
tempStr = [NSString stringWithFormat:@"%@:%@",strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
strMinute = [NSString stringWithFormat:@"%c",[tempStr characterAtIndex:1]];
tempStr = [NSString stringWithFormat:@"%@:%@",strMinute,strSecond];
NSLog(@"tempStr: %@",tempStr);

if([tempStr characterAtIndex:0] == '0')
{
tempStr = [NSString stringWithFormat:@"0:%@",strSecond];
NSLog(@"tempStr: %@",tempStr);
}
}
}
}

if([tempStrRem characterAtIndex:0] == '0')
{
strRemHour = [NSString stringWithFormat:@"%c",[tempStrRem characterAtIndex:1]];
tempStrRem = [NSString stringWithFormat:@"%@:%@:%@",strRemHour,strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
tempStrRem = [NSString stringWithFormat:@"%@:%@",strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
strRemMinute = [NSString stringWithFormat:@"%c",[tempStrRem characterAtIndex:1]];
tempStrRem = [NSString stringWithFormat:@"%@:%@",strRemMinute,strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);

if([tempStrRem characterAtIndex:0] == '0')
{
tempStrRem = [NSString stringWithFormat:@"0:%@",strRemSecond];
NSLog(@"tempStr: %@",tempStrRem);
}
}
}
}


lblTimerDisplay.text = tempStr;
lblRemainingTimeDisplay.text = [NSString stringWithFormat:@"Elapsed Time %@",tempStrRem];
}
else
{
strSecond = @"00";
strMinute = @"00";
strHour = @"00";

[timer invalidate];
}

}

No comments:

Post a Comment