如何为iPhone创建一个闪烁的录制按钮 [英] How to create a flashing record button for iPhone

查看:103
本文介绍了如何为iPhone创建一个闪烁的录制按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've looked at several posts, including [this one].1 This is exactly what I want to do, but I cannot get it to work.

我想向用户显示他们已按下一个录音按钮,他们需要再次按下它来停止录音。

I want to show the user that they have pressed a record button, and that they will need to press it again to stop recording.

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

So far, the code for the start recording method looks like this:

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方法如下所示:

and the toggleButton method looks like this:

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

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

I get to see the logs showing that the loop is iterating, but:


  1. 图片无法切换,

  2. 操作未与记录方法并行运行。

我很感激有关如何继续的任何想法。

I would appreciate any ideas as to how to proceed.

推荐答案

扩展Ashley的回答,我会选择更像这样的东西:

Expanding on Ashley's answer, I'd go with something more like this:

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

这篇关于如何为iPhone创建一个闪烁的录制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆