0

我的 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但令我惊讶的是,我看不到按钮,因为图像未在视图方法中设置。我尝试设置图像作为播放最初用于控制状态正常,然后改变播放动作。现在发生的是播放按钮没有变成暂停按钮,这是一个双重打击。我尝试设置标签,布尔值等等。没有一个似乎工作!

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

提前谢谢大家:)

4

2 回答 2

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];
}
于 2013-10-21T05:38:24.740 回答
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];
    ........
}

谢谢,希望它可以帮助一些人,快乐的编码:)

于 2013-10-21T09:26:15.363 回答