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

Xcode 如何为iPhone创建闪烁的录制按钮,xcode,ios5,uibutton,avaudiorecorder,Xcode,Ios5,Uibutton,Avaudiorecorder,我看过好几篇文章,包括[这篇]。这正是我想做的,但我无法让它发挥作用 我想向用户显示,他们已经按下了录制按钮,他们需要再次按下该按钮才能停止录制 到目前为止,开始录制方法的代码如下所示: NSLog(@"DetailVC - recordAudio - soundFilePath is %@", soundFile); [audioRecorder prepareToRecord]; recording = YES; NSLog(@"start

我看过好几篇文章,包括[这篇]。这正是我想做的,但我无法让它发挥作用

我想向用户显示,他们已经按下了录制按钮,他们需要再次按下该按钮才能停止录制

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

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");

        }
- (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;
}
- (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){
                         }];
    }
}
toggleButton方法如下所示:

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");

        }
- (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;
}
- (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){
                         }];
    }
}
我可以看到显示循环正在迭代的日志,但是:

图像不会切换,并且 该操作未与记录方法并行运行。 如果您有任何关于如何进行的想法,我将不胜感激。

试试以下方法:

- (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];
                     }];

}

根据Ashley的回答,我会选择更像这样的答案:

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");

        }
- (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;
}
- (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){
                         }];
    }
}

谢谢Ashley,你的代码看起来很酷,但我一定是做错了什么,因为它不适合我+1不过,我喜欢你用积木,当我弄明白我搞砸了什么,我会用你的片段。谢谢,马特。这对我有用!但是请注意,当alpha值小于0.02时,将忽略所有触摸事件。检查这个问题和我对它的评论@学生:对。如果需要触摸按钮,可以将alpha值保持在大于0.1的值。不会在视觉上造成太大的差异。