如何创建双功能按钮?

时间:2011-03-16 12:11:07

标签: iphone objective-c xcode

我有一个带有开始按钮(带有播放图像)的应用程序。一旦单击开始按钮,下一次单击它应该像一个暂停按钮(也是按钮图像的更改)。我们如何实现这个功能,图像改变一个用于播放,另一个用于暂停用同一个按钮?

6 个答案:

答案 0 :(得分:4)

这就是我处理它的方式

在viewController定义中定义一个bool

BOOL playing;

然后在按钮操作中,您应该执行类似

的操作
-(IBAction)playPauseButtonClicked:(id)sender
{
    if (playing)
    {
         [self pauseButtonClicked];
    }
    else
    {
         [self playButtonClicked];
    }

    UIButton *theButton = (UIButton *)sender;

    playing = !playing;
    [theButton setImage:playing ? @"pauseImage.png":@"playImage.png" forState:UIControlStateNormal];
}

-(void)pauseButtonClicked
{
    // Handle pausing
}
-(void)playButtonClicked
{
    // Handle starting to play
}

答案 1 :(得分:1)

- (IBAction)buttonClicked:(id)sender
{

 UIButton *button = (UIButton *)sender;
[button setImage:pauseImage forState:state];
//Do things
}

答案 2 :(得分:1)

全局 BOOL 不是必需的。只需为UIControlStateSelected(即播放)设置图像,为UIControlStateNormal设置另一个图像(即暂停),并在操作方法中设置:

-(IBAction)buttonAction:(id)sender{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *button = sender;
        button.selected = !button.selected;
        if (button.selected)
            // Play
        else //Pause
    }

答案 3 :(得分:0)

试试这个

-(void)playClicked:(id)sender{

UIButton *tappedButton = (UIButton*)sender;

if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"play.png"]]) {
    [sender  setImage:[UIImage imageNamed: @"pause.png"] forState:UIControlStateNormal];
}

else {
    [sender setImage:[UIImage imageNamed:@"play.png"]forState:UIControlStateNormal];
}
}

一切顺利。

答案 4 :(得分:0)

我会在click方法中有一个布尔值,它跟踪它是否已经在播放。如果它没有播放它并将图像设置为暂停图像,如果它正在播放,那么当它再次按下它做你需要做的任何事情时,即暂停和改变图像。

此循环将在您的点击方式中:

if(playing) {
    //if the button is pressed and its already playing, pause or do whatever here
    [button setImage:startImage forState:normal];
    playing = NO;
} else {
    [button setImage:pauseImage forState:normal];
    playing = YES;
}

答案 5 :(得分:0)

presentViewController