我必须制作一个从00:03:30开始的计时器程序 并停在0 在2:30秒我必须发出哔哔声如何在目标c中实现该程序
答案 0 :(得分:0)
以下是代码:
的 ViewController.h 强>
NSDate *startDate, *beepSoundDate, *endDate;
NSTimer *timer;
<强> ViewController.m 强>
-(void)setTimer
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
startDate = [dateFormatter dateFromString:@"00:03:30"];
beepSoundDate = [dateFormatter dateFromString:@"00:02:30"];
endDate = [dateFormatter dateFromString:@"00:00:00"];
// startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(getTimerInterval)
userInfo:nil
repeats:YES];
}
-(void)getTimerInterval
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setSecond:-1];
startDate = [gregorian dateByAddingComponents:offsetComponents toDate:startDate options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:startDate];
NSLog(@"%@",timeString);
if(startDate == beepSoundDate)
{
// Play beep sound
NSLog(@"************************************");
}
if(startDate == endDate)
{
[timer invalidate];
timer = nil;
}
}
如果您想开始 计时器,可以致电 setTimer 。