Как создать мигающую кнопку записи для iPhone - PullRequest
0 голосов
/ 22 декабря 2011

Я просмотрел несколько постов, включая [этот]. 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. операция не выполняется параллельно с методом записи.

Буду признателен за любые идеи о том, как поступить.

Ответы [ 2 ]

6 голосов
/ 03 января 2012

Продолжая ответ Эшли, я хотел бы пойти с чем-то вроде этого:

- (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 голос
/ 22 декабря 2011

Попробуйте это:

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

}
...