通过Cocos中的iPhone MIC检测打击,然后对图像及其内容执行动画

时间:2012-10-22 08:56:02

标签: iphone

  

可能重复:
  Detecting Blow through iPhone MIC in Cocos and then Performing animation on an image

Recipes Details Page

Hello All,我正在处理基于食谱及其详细信息的应用程序。我已经拍摄了我附加的详细页面的屏幕截图。我能够通过iPhone Mic检测到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];
    }

我担心的是,每当检测到打击和图像上的动画时,我如何管理此图像上的内容。我从昨天起就为此做了这个但是到目前为止还没能摆脱它。请建议我为此或更好地提出一些管理这个问题的代码的想法。我要感谢所有在这里帮助的人。

0 个答案:

没有答案