dodgerblue
dodgerblue

Reputation: 255

Button Events iOS

I added a button to my storyboard, and assigned a music to be played when the button is clicked. I used the event touch up inside

My question is, how can I make the button to toggle the music on and off when it is clicked.

Thanks

==================================================================================

For those of you who are interested in seeing the App, I made a repository on github. Here's the link https://github.com/edwinlimantara/IncrediboxV2. Please contribute, if you feel it is an interesting project.

I tried to make something similar to http://incredibox.com/v2/. Check it out! It is a very cool website.

Upvotes: 0

Views: 79

Answers (3)

prashant
prashant

Reputation: 1920

Better you can use UISwitch rather than button to toggle. If you want to use UIButton then you can use tags and and two different images.

if(soundButton.tag == 1)
{
 if(audioPlayer.isPlaying){
    [audioPlayer stop];
    audioPlayer = nil;
    soundButton.tag = 0; 
// Change the button image to Off
 }
 else{
    // initialize audio player and play
 }
}
else
{
    soundButton.tag = 1; 
// Play the music & Change the button image to ON

}

Upvotes: 1

spider1983
spider1983

Reputation: 1098

You can do like below, say below method is called on button tap event then,

   -(IBAction)pushClicked:(id)sender
 {
      UIButton *mButton = (UIButton*)sender;
if (mButton.selected==NO)
{
    [mButton setSelected:YES];

    //do your stuff here;
}
else if (mButton.selected==YES)
{
    [mButton setSelected:NO];
    // do your stuff here
}


}

Upvotes: 1

Gaurav Singh
Gaurav Singh

Reputation: 1997

If you are using AVAudioPlayer you can toggle the music player on button tap like:

if(audioPlayer.isPlaying){
    [audioPlayer stop];
    audioPlayer = nil;
}else{
    // initialize audio player and play
}

Upvotes: 3

Related Questions