现在,我有一个NSDate
类似“00:00.00”、“00:05.00”、“00:15.00”等的数组。我有计算NSTimerInterval
所以我可以scheduledTimerWithTimeInterval
。但是当 5 秒后安排另一个 15 秒计时器做某事时,如何安排一个嵌套计时器,然后等到日期数组结束。每个NSTimerInterval
都是从 2 个对象中计算出来的NSArray
。我可以得到第一个是这样的:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"mm:ss.SS"];
NSDate *first = [dateFormatter dateFromString:[durationArray[0]]];
NSDate *second = [dateFormatter dateFromString:durationArray[1]];
NSTimeInterval timeInterval = [second timeIntervalSinceDate:first];
[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(goToNextLine) userInfo:nil repeats:NO];
如果我做一个for循环,计时器不会一个接一个地触发:
for (int i = 0; i<[durationArray count]; i++) {
int j = i+1;
if (j == [durationArray count]) {
} else
{
NSDate *first = [dateFormatter dateFromString:durationArray[i]];
NSDate *second = [dateFormatter dateFromString:durationArray[j]];
NSTimeInterval timeInterval = [second timeIntervalSinceDate:first];
[NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(goToNextLine) userInfo:nil repeats:NO];
}
}