2011-12-22 53 views
0

我看了幾篇文章,包括[this one]。 1這正是我想要做的,但我無法讓它工作。如何爲iPhone創建閃爍的錄製按鈕

我想向用戶顯示他們已經按下了一個錄製按鈕,並且他們需要再次按下才能停止錄製。

迄今爲止,開始記錄方法的代碼如下所示:

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

我能看到顯示出循環日誌被迭代,但:

  1. 圖像不切換,並且
  2. 該操作不與記錄方法並行運行。

我將不勝感激任何想法如何繼續。

回答

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

謝謝你,馬特,對我有用。 – 2012-01-07 20:12:34

+0

沒問題。很高興它對你有效。 – mattjgalloway 2012-01-08 11:00:27

+0

謝謝,馬特。它爲我工作!但請注意,當alpha達到0.02以下時,所有觸摸事件都將被忽略。 檢查這個問題和我對它的評論。 http://stackoverflow.com/questions/13499817/does-uibutton-become-disabled-when-its-alpha-is-set-to-0-0 – CSawy 2014-08-13 18:16:38

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

} 
+0

感謝阿什利,你的代碼看起來很酷,但我必須做一些別的錯誤,因爲它不適合我。儘管+1,因爲我喜歡你對塊的使用,當我弄清楚我已經搞砸了什麼,我會用你的代碼片斷。 – 2011-12-22 19:18:34