Как да създадете мигащ бутон за запис за iPhone

Разгледах няколко публикации, включително [тази].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. операцията не се изпълнява паралелно с метода на запис.

Ще се радвам на всякакви идеи как да продължа.


person ICL1901    schedule 22.12.2011    source източник


Отговори (2)


Разширявайки отговора на Ашли, бих избрал нещо по-подобно на това:

- (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){
                         }];
    }
}
person mattjgalloway    schedule 03.01.2012
comment
Благодаря, Мат. При мен се получи! Обърнете внимание обаче, че когато алфа достигне по-малко от 0,02, всички събития на докосване се игнорират. проверете този въпрос и коментара ми по него. stackoverflow.com/questions/13499817/ - person CSawy; 13.08.2014
comment
@CSstudent : Точно така. Можете да запазите алфата на нещо по-голямо от 0,1, ако имате нужда от докосвания на бутона. Не би създало голяма разлика визуално. - person Mohamed Haseel; 08.01.2016

Опитайте тази:

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

}
person Ashley Mills    schedule 22.12.2011
comment
Благодаря, Ашли, кодът ти изглежда готин, но трябва да правя нещо друго грешно, тъй като не работи за мен.. +1 все пак, тъй като ми харесва използването на блокове от теб и когато разбера какво съм объркал, ще използвайте своя фрагмент. - person ICL1901; 22.12.2011