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

时间:2013-10-21 05:20:52

标签: iphone image uibutton

我的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;
}

这是播放按钮操作:

-(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方法中进行操作。但是,令我惊讶的是,由于未在视图方法中设置图像,因此无法看到按钮。我试图将图像设置为最初为控制状态正常播放然后更改播放动作。现在发生的是播放按钮没有转到暂停按钮,这是一个双重打击。我尝试设置标签,布尔价值观和更多。他们似乎没有工作!!!

任何人都可以建议我一个解决方案吗?

提前感谢所有人:)

2 个答案:

答案 0 :(得分:1)

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

将图像实例设置为状态UIControlStateNormal,但播放电影时按钮状态仍为UIControlStateSelected(即将显示pause.png)。

所以解决方法是,你需要设置状态。

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

答案 1 :(得分:0)

就在那时,我找到了解决问题的方法。感谢Mr.San先生和Mr.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];
    ........
}

谢谢,希望它有助于一个人,快乐编码:)