简体   繁体   English

选择另一个按钮后更改UIButton图像

[英]Change UIButton image upon another button selection

I have 2 buttons in my UIView underneath a video,a play and a stop button.When Play button is clicked,it turns out to pause button and vice-versa.Here is the implementation code: 我的UIView中有2个按钮,分别是视频,播放和停止按钮。单击“播放”按钮时,原来是暂停按钮,反之亦然。这是实现代码:

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

and here is the play button action: 这是播放按钮动作:

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

My requirement is I need to change the play image to pause for play button once the video is stopped ie when user clicks stop button.I have tried the below way: 我的要求是,一旦视频停止(即用户单击停止按钮),我需要将播放图像更改为暂停播放按钮。我尝试了以下方法:

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

It is validating the condition in stop button action,I have already debugged and tested,but the image change doesn't affect.Wonder the reason why?? 正在验证停止按钮动作中的条件,我已经调试和测试了,但是图像变化不受影响。为什么会这样?

I have also tried changing the images in play button action instead of doing in -(UIView *)subViewControlsView method.But what happens is to my surprise,I can't see the button as the image is not set in the view method.I have tried to set up the image as play initially for control state normal and then change in play action.Now what happens is the play button doesn't turn to pause button,which is a double blow.I tried setting tags,boolean values and lot more.None of them seem to work!!! 我也尝试过在播放按钮操作中更改图像,而不是在-(UIView *)subViewControlsView但是发生的事情令我惊讶,我无法看到按钮,因为未在view方法中设置图像。尝试将图片设置为最初处于控制状态正常的播放状态,然后更改播放动作。现在发生的情况是播放按钮没有变成暂停按钮,这是双重打击。我尝试设置标签,布尔值和还有很多,他们似乎都不起作用!

Can any one please suggest me a solution? 有人可以给我建议解决方案吗?

Thanks all in advance :) 提前谢谢大家:)

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

Will set the image instance to state UIControlStateNormal, but the button state is still UIControlStateSelected when playing the movie (ie pause.png will be displayed). 将图像实例设置为状态UIControlStateNormal,但在播放电影时按钮状态仍为UIControlStateSelected(即,将显示pause.png)。

So the solution is, you need to set the state instead. 因此解决方案是,您需要设置状态。

-(void)stop:(UIButton *)sender{
    if (playButton.isSelected)
    {
        [playButton setSelected:NO];
    }
    self.currentTime = 0.0;
    [[self moviePlayerController] stop];
    [self deletePlayerAndNotificationObservers];
}

Right then,I have found a solution to my problem.Thanks to Mr.San and Mr.Preetam for their concern and helping me out in getting rid of the issue.We need to change the button image by setting the tag and then disabling the selected state of playButton ie, 那时,我找到了解决问题的方法。感谢San先生和Preetam先生的关心,并帮助我摆脱了这个问题。我们需要通过设置标签来更改按钮图像,然后禁用playButton的选定状态,即

//Set the tag for button
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
playButton.tag = 104;

Then access the button and reset its state to normal 然后访问按钮并将其状态重置为正常

-(void)stop:(UIButton *)sender
{
    UIButton * play=(UIButton*)[self.view viewWithTag:104];
    [play setSelected:NO];
    ........
}

Thanks and hope it helps some one,Happy coding :) 谢谢,希望对您有所帮助,祝您编程愉快:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM