我在我的UIView中有一個視頻,一個播放和一個停止按鈕下的2個按鈕。當單擊播放按鈕時,它變成暫停按鈕並反向調用。這裏是實現代碼:在另一個按鈕選擇上更改UIButton圖像
-(UIView *)subViewControlsView{
UIView *subViewWithControls = [[[UIView alloc]init]autorelease];
subViewWithControls = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - xPoint_Pad) - 147, 630, 147, 44)];
[subViewWithControls setBackgroundColor:[UIColor clearColor]];
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton setFrame:CGRectMake(0, 0, 44, 44)];
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
[playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[subViewWithControls addSubview:playButton];
stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
[stopButton setFrame:CGRectMake(94, 0, 44, 44)];
[stopButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
[stopButton addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
[subViewWithControls addSubview:stopButton];
return subViewWithControls;
}
,這裏是播放鍵操作:
-(void)play:(UIButton *)sender
{
if(sender.selected == NO){
sender.selected = YES;
[self setButtonCurrentState:YES];
[self.moviePlayerController setInitialPlaybackTime:self.currentTime];
[self playMovieFile:[self localMovieURL]];
}
else{
sender.selected = NO;
[self setButtonCurrentState:NO];
self.currentTime = self.moviePlayerController.currentPlaybackTime;
[self.moviePlayerController pause];
}
}
我的要求是,我需要改變播放圖像暫停播放按鈕,當影片播放即停止,當用戶點擊停止按鈕。我嘗試了以下方法:
-(void)stop:(UIButton *)sender{
if (self.buttonCurrentState == YES)
{
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
}
self.currentTime = 0.0;
[[self moviePlayerController] stop];
[self deletePlayerAndNotificationObservers];
}
它正在驗證停止按鈕動作中的條件,我已經調試和測試過了,但圖像變化沒有影響。想知道爲什麼?
我也嘗試改變播放按鈕操作中的圖像,而不是在-(UIView *)subViewControlsView
method.But發生什麼是令我驚訝的是,我看不到該按鈕,因爲該圖像未在視圖方法中設置。我有試圖設置圖像作爲最初控制狀態正常播放,然後改變播放action.Now發生了什麼是播放按鈕不轉到暫停按鈕,這是一個雙重打擊。我試着設置標籤,布爾值和很多more.None他們似乎工作!
任何人都可以請給我一個解決方案嗎?
感謝所有提前:)
感謝您的答案,但對不起,它沒有工作! –
你是否設法檢查playButton是否沒有對不對?因爲我沒有看到任何按鈕不會更改圖像的問題。 – San
我正在更改播放按鈕的圖像,即它最初保存播放圖像,然後單擊時變成暫停圖像,反之亦然,該部分工作正常!但是,當我通過停止按鈕動作停止視頻時,我希望帶有暫停圖像的播放按鈕可以轉到播放圖像。這是迄今爲止我無法實現的目標! –