简体   繁体   English

按钮事件iOS

[英]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 我用touch up inside的事件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. 对于那些有兴趣查看该应用程序的人,我在github上建立了一个存储库。 Here's the link https://github.com/edwinlimantara/IncrediboxV2 . 这是链接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/ . 我试图制作类似于http://incredibox.com/v2/的内容 Check it out! 看看这个! It is a very cool website. 这是一个非常酷的网站。

If you are using AVAudioPlayer you can toggle the music player on button tap like: 如果您使用的是AVAudioPlayer,则可以在按钮点击时切换音乐播放器,例如:

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

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
}


}

Better you can use UISwitch rather than button to toggle. 更好的是,您可以使用UISwitch而不是按钮进行切换。 If you want to use UIButton then you can use tags and and two different images. 如果要使用UIButton,则可以使用标签和和两个不同的图像。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM