我看了幾篇文章,包括[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;
}
我能看到顯示出循環日誌被迭代,但:
- 圖像不切換,並且
- 該操作不與記錄方法並行運行。
我將不勝感激任何想法如何繼續。
謝謝你,馬特,對我有用。 – 2012-01-07 20:12:34
沒問題。很高興它對你有效。 – mattjgalloway 2012-01-08 11:00:27
謝謝,馬特。它爲我工作!但請注意,當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