Iphone 在选择其他按钮时更改UIButton图像
在我的UIView中,视频下方有两个按钮,一个播放按钮和一个停止按钮。单击播放按钮时,结果显示为暂停按钮,反之亦然。以下是实现代码:Iphone 在选择其他按钮时更改UIButton图像,iphone,image,uibutton,Iphone,Image,Uibutton,在我的UIView中,视频下方有两个按钮,一个播放按钮和一个停止按钮。单击播放按钮时,结果显示为暂停按钮,反之亦然。以下是实现代码: -(UIView *)subViewControlsView{ UIView *subViewWithControls = [[[UIView alloc]init]autorelease]; subViewWithControls = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame
-(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];
}
它正在验证停止按钮动作的条件,我已经调试和测试过了,但是图像的变化没有影响。想知道为什么
我也尝试过在play button action中更改图像,而不是在-(UIView*)子视图ControlsView
方法中进行更改。但发生的事情令我惊讶,我看不到按钮,因为图像未在视图方法中设置。我尝试将图像设置为“控制状态正常”的初始播放,然后在播放操作中更改。现在发生的情况是“播放”按钮没有变为“暂停”按钮,这是一个双重打击。我尝试设置标记、布尔值等。它们似乎都不起作用
谁能给我一个解决方案吗
提前感谢大家:)
将图像实例设置为状态UIControlStateNormal,但播放电影时按钮状态仍为UIControlStateSelected(即将显示pause.png)
因此,解决方案是,您需要设置状态
-(void)stop:(UIButton *)sender{
if (playButton.isSelected)
{
[playButton setSelected:NO];
}
self.currentTime = 0.0;
[[self moviePlayerController] stop];
[self deletePlayerAndNotificationObservers];
}
现在,我找到了解决问题的方法。感谢San先生和Preetam先生的关心,并帮助我解决了问题。我们需要通过设置标签,然后禁用playButton的选定状态来更改按钮图像,即
//Set the tag for button
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
playButton.tag = 104;
然后访问按钮并将其状态重置为正常
-(void)stop:(UIButton *)sender
{
UIButton * play=(UIButton*)[self.view viewWithTag:104];
[play setSelected:NO];
........
}
感谢并希望它能帮助一些人,快乐编码:)您是否检查了playButton是否为零?因为我看不到按钮没有改变图像的任何问题。我正在改变播放按钮的图像,即它最初保存播放图像,然后当单击时会变成暂停图像,反之亦然,该部分工作正常!!但当我通过停止按钮动作停止视频时,我希望带有暂停图像的播放按钮变为播放图像。这是我迄今为止无法实现的!!
-(void)stop:(UIButton *)sender
{
UIButton * play=(UIButton*)[self.view viewWithTag:104];
[play setSelected:NO];
........
}