我正在开发一个与食谱相关的应用程序。在这个应用程序中有一个部分,用户可以通过麦克风吹气,并可以通过使用动画转换 CurlDown 更改图像以及其上的内容。我能够检测到使用以下代码进行打击,
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target: self selector: @selector(levelTimerCallback:) userInfo:nil repeats:YES];
}
else{
// NSLog([error description]);
}
image =[[UIImageView alloc] init];
image.image =[UIImage imageNamed:@"Recipie.png"];
image.frame =CGRectMake(50, 100, 150, 200);
[self.view addSubview:image];
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
// NSLog(@"Average input: %f Peak input: %f Low pass results: %f", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0], lowPassResults);
if (lowPassResults >0.055 )
{
NSLog(@"Mic blow detected");
[self changeFrame];
}
}
-(void)changeFrame
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:image cache:NO];
[UIView commitAnimations];
}
但我的问题是,当我第一次通过麦克风吹气时,图像会动画 5 到 7 次,而当我第二次吹气时,图像会动画 9 到 10 次,我希望每次检测到这个动画时都会播放一次。请建议我如何使用此代码执行此操作,或者如果有人可以共享本节的代码,对我来说会更好。