【问题标题】:Button Events iOS按钮事件 iOS
【发布时间】:2013-12-30 05:47:06
【问题描述】:

我在情节提要中添加了一个按钮,并指定了在单击该按钮时播放的音乐。我用事件touch up inside

我的问题是,我怎样才能让按钮在点击时打开和关闭音乐。

谢谢

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

对于那些有兴趣查看该应用程序的人,我在 github 上创建了一个存储库。这是链接https://github.com/edwinlimantara/IncrediboxV2。如果您觉得这是一个有趣的项目,请投稿。

我尝试制作类似于http://incredibox.com/v2/ 的东西。看看这个!这是一个非常酷的网站。

【问题讨论】:

    标签: ios iphone objective-c


    【解决方案1】:

    如果您使用的是 AVAudioPlayer,您可以在点击按钮时切换音乐播放器,例如:

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

    【讨论】:

    • 哦,非常感谢,我之前不知道 .isPlaying
    【解决方案2】:

    你可以像下面这样,说下面的方法在按钮点击事件上被调用,

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

    【讨论】:

    • 在 AVAudioPlayer 中使用 .isPlaying 可能会更好。还是谢谢你的回答
    【解决方案3】:

    你可以使用 UISwitch 而不是按钮来切换更好。 如果你想使用 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
    
    }
    

    【讨论】:

    • 这是个好主意。我还可以更改图像,那就更好了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多