如何为iPhone创建一个闪烁的录制按钮

时间:2011-12-22 15:09:48

标签: xcode ios5 uibutton avaudiorecorder

我看过几个帖子,包括[这一个]。1这正是我想要做的,但我无法让它发挥作用。

我想向用户显示他们已按下录制按钮,并且他们需要再按一次以停止录制。

到目前为止,开始录制方法的代码如下所示:

NSLog(@"DetailVC - recordAudio -  soundFilePath is %@", soundFile);
        [audioRecorder prepareToRecord];
        recording = YES;
        NSLog(@"start recording");
        NSLog(@"1");
        //[autoCog startAnimating];

        [audioRecorder recordForDuration:120];
        recording = NO;

        //Start a timer to animate the record images
        if (recording == YES) {
        toggle = FALSE;
        timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
                                                          target: self
                                                        selector: @selector(toggleButtonImage:)
                                                        userInfo: nil
                                                         repeats: YES];  


        //UIImage *changeImage = [UIImage imageNamed:stopButtonFile];       
        //[recordButton setImage:changeImage forState:UIControlStateNormal];        


        //[timer invalidate];
        //timer = nil;

        NSLog(@"2");

        }

并且toggleButton方法如下所示:

- (void)toggleButtonImage:(NSTimer*)timer
{
   NSLog(@"%s", __FUNCTION__);
    if(toggle)
    {
        NSLog(@"1");
        /*
        [UIView beginAnimations];
        recordButton.opacity = 0.0;
        [recordButton setAnimationDuration: 1.5];
        [recordButton commitAnimations];
        [recordButton setImage:[UIImage imageNamed:@"record.png"] forState: UIControlStateNormal];
         */
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //delete "EaseOut", then push ESC to check out other animation styles
        [UIView setAnimationDuration: 0.5];//how long the animation will take
        [UIView setAnimationDelegate: self];
        recordButton.alpha = 1.0; //1.0 to make it visible or 0.0 to make it invisible
        [UIView commitAnimations];
    }
    else 
    {
         NSLog(@"2");
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //delete "EaseOut", then push ESC to check out other animation styles
        [UIView setAnimationDuration: 0.5];//how long the animation will take
        [UIView setAnimationDelegate: self];
        recordButton.alpha = 0.0; //1.0 to make it visible or 0.0 to make it invisible
        [UIView commitAnimations];
        //[recordButton setImage:[UIImage imageNamed:@"stopGrey.png"] forState: UIControlStateNormal];
    }
    toggle = !toggle;
}

我看到日志显示循环正在迭代,但是:

  1. 图片无法切换,
  2. 操作未与记录方法并行运行。
  3. 我很感激有关如何继续的任何想法。

2 个答案:

答案 0 :(得分:6)

扩展阿什利的答案,我会选择更像这样的东西:

- (IBAction)record:(id)sender {
    self.recording = !self.recording;

    if (!self.recording) {
        [UIView animateWithDuration:0.1 
                              delay:0.0 
                            options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         self.recordButton.alpha = 1.0f;
                     } 
                     completion:^(BOOL finished){
                     }];
    } else {
        self.recordButton.alpha = 1.0f;
        [UIView animateWithDuration:0.5 
                              delay:0.0 
                            options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction 
                         animations:^{
                             self.recordButton.alpha = 0.0f;
                         } 
                         completion:^(BOOL finished){
                         }];
    }
}

答案 1 :(得分:1)

试试这个:

- (IBAction)record:(id)sender 
{
    self.recording = !self.recording;

    [self toggleButton];
}

- (void) toggleButton
{
    if (!self.recording) {
        self.recordButton.alpha = 1.0;
        return;
    }

    [UIView animateWithDuration: 0.5 
                          delay: 0.0 
                        options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction 
                     animations:^{
                         self.recordButton.alpha = !self.recordButton.alpha;
                     } 
                     completion:^(BOOL finished) {
                         [self toggleButton];
                     }];

}