开发者

How can I create a dual function button?

I have a application which has a start button(with a play image). once the start button is clicked the next click it should act like a pause button (also a change in button image). How can we implement both this function wit开发者_开发问答h image changing one for play and another for pause with the same button?


This is how i'd handle it

In your viewController definition define a bool

BOOL playing;

then in the button action you should do something like

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


- (IBAction)buttonClicked:(id)sender
{

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


The global BOOL is not necessary. Simply set an image for UIControlStateSelected (i.e. Play) and another for UIControlStateNormal (i.e. pause) and in your action method:

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


Try this

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

All the best.


I would have a boolean in the click method that keeps track if it is playing already or not. If it is not playing start it and set the image to the pause image, if it is playing then when its pressed again do whatever you need it to do, i.e. pause and change image.

This loop would be in your click method:

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


    Here is my code. Have a look.

    @IBAction func playAndPauseButtonClicked(sender: AnyObject) {
            let skView = self.view as! SKView
            skView.scene!.paused = true

            if (playAndPauseButton.currentImage == UIImage(named: "play.png"))
            {
                playAndPauseButton.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal)
            }
            else
            {
                playAndPauseButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
                skView.scene!.paused = false
            }

        }

Note: I used this in a sample game where I have button which pause and play the game scene. 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜