서버에서받는 문자열에서 카운트 다운되는 타이머를 만들어야합니다. 문자열은 남은 총 시간 (초)을 출력합니다.시간 계산이 점점 더 잘못된 결과를 내고 있습니다.
나는이 코드를 사용하고 있습니다. 처음 숫자를 올바르게 계산하고 카운트 다운하지만 분이 문제입니다.
편집 - 실제 순간마다 1 분씩 계산이 올라갑니다. 그러나 나는 왜이 일을하는지 이해하지 못합니다.
- (무효) showTimmer (ID) 보낸 사람 {
//get total amount of seconds
NSString *timeRaw = [ArrayFromServer objectAtIndex:1];
NSArray *timeArr = [timeRaw componentsSeparatedByString:@","];
timetoInt = [timeArr objectAtIndex:0];
int time1 = [timetoInt intValue];
//set the second countdown
NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSSecondCalendarUnit) fromDate:now];
NSInteger seconds = time1 - [dateComponents second];
[gregorian release];
// do the hours days maths
int hour = (seconds/3600);
seconds -= hour * 3600;
int minute = (seconds/60);
seconds -= minute * 60;
int second = seconds;
//set the labels
countdownLabel.text = [NSString stringWithFormat:@"%02dH %02dM %02ds", hour, minute, second];
또 다른 옵션은 시작 시간을 NSDate로 저장 한 다음 NSInteger elapsedTime = (NSInteger) [startDate timeIntervalSinceNow]; –
을 수행하는 것입니다.이 작업은 완벽하게 작동합니다. – jaggi
네, NSDateComponents가 무엇을하는지 알기 전까지는 반올림 오류라고 생각했습니다. 그 클래스와 그것의 동료들에 관한 문서들은 확실히 간결한 편입니다. (개인적으로는 [NSCalendar-firstWeekday'] (http://developer.apple.com.덧붙여서, 당신은'double's를 사용하려고 시도 할 것입니다. 왜냐하면, 여러분은'double's를 시도 할 것입니다. 그것은'CF-'와'NS-''TimeInterval' 둘 다입니다. 그런 다음 필요에 따라'fmod','ceil' 및'floor'를 사용하십시오. 어쨌든, 내가 도울 수있어서 기뻐요! –