0
試圖解決我的Other question。這次嘗試這種方式 爲一個按鈕添加了多個方法,但是當點擊按鈕時,它看起來沒有任何東西看起來像這些方法不起作用。多種方法:按鈕的目標和選擇器不工作
//Add Play Button
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
- (void)playAction:(id)sender{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
-(void)playpauseAction:(id)sender {
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:@"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:@selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
}
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
它顯示播放按鈕時,點擊它沒有任何反應。它應播放音頻文件,啓動NSTimer,開始加載視圖並在播放和暫停按鈕之間切換。任何想法是什麼錯誤。
謝謝。
不,它不暫停音頻播放器,因爲它應該顯示沒有發生的暫停圖標。 – user1452248
@ user1452248您在if語句的同一分支中爲UIControlStateSelected設置了「Play Icon.png」。 – thelaws
好吧,我已經切換了方法的順序[playpauseButton addTarget:self action:@selector(playpauseAction :) forControlEvents:UIControlEventTouchUpInside]; [playpauseButton addTarget:self action:@selector(playAction :) forControlEvents:UIControlEventTouchUpInside];現在它正在播放音頻文件,在播放暫停按鈕之間切換,但現在暫停按鈕不會切換回播放按鈕,如果再次按下恢復 – user1452248