每次我尝试用CFAbsoluteTimeGetCurrent()记录日期时都好;我的应用程序忽略了其余的按钮,就好像它占用了所有内存并阻止了所有用户输入。我想知道我做错了什么?我想过制作一个函数showtime()但是我不知道如何在toggleRecording to showtime之间传递函数值,这样我的方法就可以解决问题了。以下是我的代码:
- (IBAction)toggleRecording:(id)sender
{
// Start recording if there isn't a recording running. Stop recording if there is.
[[self recordButton] setEnabled:NO];
if (![[[self captureManager] recorder] isRecording]){
[[self captureManager] startRecording];
/* figure out a day to record for every half a second
while([[[self captureManager]recorder] isRecording]){
CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
NSLog(@" time is %i", startTime);
}
*/
}
else
[[self captureManager] stopRecording];
}
-(void)showtime:(id)sender{
while([[[self captureManager]recorder] isRecording]){
CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
NSLog(@" time is %f", startTime);
}
}
答案 0 :(得分:2)
应用程序必须运行事件循环才能接收事件。虽然您自己的代码正在执行其他操作(此处它正在执行“while”循环),但事件会排队,并且在您的代码返回之前不会传递。
示意图,应用程序正在执行以下操作:
while(1) {
event = _UIReceiveNextQueuedEvent();
_UIProcessEvent(event); // <- this calls -showTime:
}
如果您想在不阻止循环的情况下记录时间,则必须每隔0.5秒安排一次NSTimer
,并在录音关闭后立即使其失效。
类似的东西:
- (void)showTime:(id)sender
{
if ([[[self captureManager]recorder] isRecording]) {
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
}
- (void)timerFired:(NSTimer *)timer
{
if ([[[self captureManager]recorder] isRecording]) {
CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
NSLog(@" time is %f", startTime);
} else {
[timer invalidate];
}
}