2012-03-26 76 views
0

我一直在搜索互联网几天到现在。我确实找到了一些有用的信息,但只是想让事情变得完美。Iphone应用风格倒数改进?

我正在尝试写一个倒数计时器应用程序,它只使用第二个应用程序,我现在的代码有点太复杂,可能不在主要道路上。寻找一个能够把它弄直的人。

- (void)updateTimer{ 

NSDate *currentDate = [NSDate date]; 
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"s"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
NSString *timeString=[dateFormatter stringFromDate:timerDate]; 

// where the part change time string back to int 
NSString *changeToInt = timeString; 
int stringInt = [changeToInt integerValue]; 

// loop for count down numbers 
if ((buttValue - stringInt) > 0) { 
    leftTime = buttValue - stringInt; 
    [self updateValue]; 
} else { 
    leftTime = 0; 
    [self updateValue]; 
} 

}

2问题的代码,1)我必须通过所有的日期格式得到一个整数? 2)在循环中我使用了两个变量,我希望能拿出一个并使用类似的东西 -

回答

0

我不明白你需要什么,但我认为你只是想获得秒数从现在到一个已知的日期。 timeIntervalSinceNow方法将为您提供一个NSInteger值,它表示从接收方日期到现在的秒数,如果接收方比现在早,它将是负值(否则将为正值)。

所以,只是为了更加明确:

- (void)updateTimer 
{ 
    leftTime = MAX(0, -[startDate timeIntervalSinceNow]); //if less than 0 will be forced to 0 -> date reached 
    [self updateValue]; 
} 

正如我所说的,我不知道这是你想要的。如果不是,我很抱歉。 祝你好运!

+0

我是这样一个傻瓜,我不明白scheduledTimerWithTimeInterval是如何工作的。如果((buttValue-1)> 0)buttValue = buttValue-1;}如果((buttValue-1)> 0),我现在将代码更改为反向倒计数 。 [self updateValue]; } else { buttValue = 0; [self updateValue]; [timer invalidate]; }' – oursonvie 2012-03-26 09:42:13

0
-(void)initialiseTimer 
{ 
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeElapsed) userInfo:nil repeats:YES]; 
    startDate = [[NSDate date] retain]; 

} 
-(void)timeElapsed 
{ 

    NSLog(@"Seconds elapsed since start date %d", abs([startDate timeIntervalSinceNow])); 

} 

你可以操纵它来做倒计时风格的应用程序。

0

使用此代码!

-(IBAction)startAndStop:(id)sender 
{ 
    startDate = [[NSDate date]retain]; 
    if([btnStopWatch.titleLabel.text isEqualToString:@"Start"]) 
    { 
     stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/100.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 
     [btnStopWatch setTitle:@"Stop" forState:UIControlStateNormal]; 
    } 
    else if([btnStopWatch.titleLabel.text isEqualToString:@"Stop"]) 
    { 
     [stopWatchTimer invalidate]; 
     [btnStopWatch setTitle:@"Start" forState:UIControlStateNormal]; 
    } 
} 
- (void)updateTimer 
{ 
    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    timeString=[dateFormatter stringFromDate:timerDate]; 
    stopWatchLabel.text = timeString; 
    NSLog(@"Seconds elapsed since start date %d", abs([startDate timeIntervalSinceNow])); 

}