0

可能重复:
在 Cocos 中通过 iPhone MIC 检测 Blow,然后在图像上执行动画

食谱详情页面

大家好,我正在开发基于食谱及其详细信息的应用程序。我已经拍摄了我附在此处的详细信息页面的屏幕截图。我能够通过 iPhone 麦克风检测到 Blow 并且还能够将我在项目类中命名为“pageImage”的图像动画化。使用下面的代码

   -(void)viewDidLoad

      [self paperDetails];


   -(void)paperDetails
    {
        NSMutableArray *arrayNum =[[NSMutableArray alloc] init];
        [arrayNum addObject:@"1:"];
        [arrayNum addObject:@"2:"];
        [arrayNum addObject:@"3:"];
        [arrayNum addObject:@"4:"];

        CGFloat xOffset=0;
        CGFloat yOffset=0;
        CGFloat imgWidth =31.0;
        CGFloat imgHeight =30.0;
        int rowNumber =0;
        int imgInc = 0;
        for (int i=0; i<[arrayNum count]; i++) {
            //CALCULATE xOffset FOR EACH IMAGE
            if (i%1 == 0) {
                xOffset =15;
            }
            //CALCULATE yOffset FOR EACH IMAGE
            if (imgInc == 1) {
                imgInc = 0;
                rowNumber = rowNumber + 1;
            }

            imgInc = imgInc+1;
            yOffset =(imgHeight+30)* rowNumber;
            NSLog(@"yOffset   %f",yOffset);

            UILabel *recipeTitle = [[UILabel alloc]init];
            [recipeTitle setTag:i];
            [recipeTitle setBackgroundColor:[UIColor clearColor]];
            [recipeTitle setFrame:CGRectMake(xOffset, yOffset+100, imgWidth, imgHeight)];
            //titleLabel.text=[[appDelegate.activeCardArr objectAtIndex:i] objectForKey:@"Card_Title"];
            recipeTitle.text =[arrayNum objectAtIndex:i];
            recipeTitle.textColor =[UIColor colorWithRed:0 green:0.392 blue:0 alpha:1];
            recipeTitle.font =[UIFont fontWithName:@"Noteworthy-Bold" size:23.0];
            recipeTitle.textAlignment =UITextAlignmentCenter;
            [paperImage addSubview:recipeTitle];
            [recipeTitle release];
        }

        for (int i=0; i<=3; i++) {

            //CALCULATE xOffset FOR EACH IMAGE
            if (i%1 == 0) {
                xOffset =50;
            }
            //CALCULATE yOffset FOR EACH IMAGE
            if (imgInc == 1) {
                imgInc = 0;
                rowNumber = rowNumber + 1;
            }

            imgInc = imgInc+1;
            yOffset =(imgHeight+30)* rowNumber;
            NSLog(@"yOffset   %f",yOffset);
            UILabel *paperDetail = [[UILabel alloc]init];
            [paperDetail setBackgroundColor:[UIColor clearColor]];
            [paperDetail setFrame:CGRectMake(xOffset, yOffset-165, imgWidth+250, imgHeight+50)];
            //titleLabel.text=[[appDelegate.activeCardArr objectAtIndex:i] objectForKey:@"Card_Title"];
            paperDetail.text =@"keep stirring until cooked";
            paperDetail.textColor =[UIColor colorWithRed:0.2 green:0 blue:0.098 alpha:1];
            paperDetail.font =[UIFont fontWithName:@"Noteworthy-Bold" size:23.0];
            paperDetail.textAlignment =UITextAlignmentCenter;
            [paperImage addSubview:paperDetail];
            [paperDetail release];

        }
       //*************Blow Detection Code Here **********//

        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:0.005 target: self selector: @selector(levelTimerCallback:) userInfo:nil repeats:YES];


        }
        else{
            //       NSLog([error description]);
        }
    }
    - (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;
        if (lowPassResults >0.55)
        {
            if (!self.blowDetected) {
                self.blowDetected = TRUE;
    //            NSLog(@"Mic blow detected");
                [self changeFrame];
            }
        } else
        {
    //        NSLog(@"Blow not detected with residual power: %f", lowPassResults);
            self.blowDetected = FALSE;
        }
    }

    -(void)changeFrame
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:paperImage cache:NO];
        [UIView commitAnimations];
    }

我担心的是,每次检测到打击并在图像上执行动画时,我必须如何管理此图像上的内容。我从昨天开始为此做 rnd,但直到现在都无法摆脱。请建议我一些想法这个或更好的代码来管理这个问题。我会感谢所有在这里提供帮助的人。

4

0 回答 0