简体   繁体   English

单击另一个按钮swift 2.2更改声音按钮图像

[英]Change sound button image on clicking another button swift 2.2

I am creating a Tableview inside which is Tableviewcell and on cell there is a label and sound button. 我正在创建一个Tableview里面的Tableviewcell,在单元格上有一个标签和声音按钮。 For each label there is a sound on button click. 对于每个标签,按钮单击时会发出声音。 When I click for the first time on btn1 the sound plays and the button image changes to "pause" when I click again the same button sound stops and the image changes to "play" works perfectly in this manner but when I click for the first time on one button let suppose btn1 and without clicking it again (stoping sound) I click on btn2, the sound of the btn1 stops and the image of btn1 nor btn2 changes. 当我第一次点击btn1时,声音播放,按钮图像变为“暂停”,当我再次点击时,相同的按钮声音停止并且图像变为“播放”以这种方式完美地工作但是当我点击第一次时在一个按钮上的时间让我们假设btn1而不再点击它(停止声音)我点击btn2,btn1的声音停止,btn1和btn2的图像也发生变化。 I want that when I click on btn 2,3, or 4 the previous sound should stop, the image of previous button (means all buttons except the current) should change to "play" and the current clicked button should change to "pause" and the sound of the previous click should stop and the current clicked should play. 我希望当我点击btn 2,3或4前一个声音应该停止时,前一个按钮的图像(表示除了当前的所有按钮)应该变为“播放”,当前点击的按钮应该变为“暂停”并且应该停止前一次点击的声音并且应该播放当前点击的声音。

import UIKit

class TableViewCell: UITableViewCell {

    @IBOutlet weak var titleLable: UILabel!
    @IBOutlet weak var sound: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate , GADInterstitialDelegate {

    var countsNumberOfButtonClicks = 0
    var countsNumberOfInfoBtnClicks = 0
    var isFirstTime = false
    var player : AVAudioPlayer! = nil
    var titleAlert: String!

    @IBOutlet weak var myTableView: UITableView!
    var toggleState = 1


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell.

    {

    let myCell = self.myTableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! TableViewCell

        myCell.titleLable.text = self.Duck[indexPath.row]

        myCell.sound.tag = indexPath.row
        myCell.sound.addTarget(self, action: #selector(self.playSound), forControlEvents: .TouchUpInside)
     return myCell
    }

   @IBAction func playSound(sender: UIButton) {

            if toggleState == 1 {
            let fullName: String = self.Duck[sender.tag]
            let fullNameArr = fullName.componentsSeparatedByString(" ")
            let path = NSBundle.mainBundle().pathForResource(fullNameArr[0], ofType:"wav", inDirectory: "sounds")
            let fileURL = NSURL(fileURLWithPath: path!)
            do {
                player = try AVAudioPlayer(contentsOfURL: fileURL)
                player.prepareToPlay()
            } catch {
                print("Problem in getting File")
              }   
            player.play()
            sender.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal)
            print("toggle state 1")
            toggleState = 2
        }
        else {
            player.pause()
            toggleState = 1
            sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
            print("Toggle state else")
 }

Simulator result 模拟器结果

In you class, declare a variable, 在你的类中,声明一个变量,

var currentlyPlaying : UIButton?

And wherever you play the sound, store the button which played the sound in this variable and whenever this variable is about to be change, reset the previous ones image and store the new one. 无论您在何处播放声音,都要存储在此变量中播放声音的按钮,并且每当此变量即将更改时,请重置上一个图像并存储新图像。

@IBAction func playSound(sender: UIButton) {

        if currentlyPlaying != sender || toggleState == 1 {

        //Set image in previous
        currentlyPlaying?.setImage(UIImage(named: "play.png")

        if player != nil{
            player.pause()
        }

        let fullName: String = self.Duck[sender.tag]
        let fullNameArr = fullName.componentsSeparatedByString(" ")
        let path = NSBundle.mainBundle().pathForResource(fullNameArr[0], ofType:"wav", inDirectory: "sounds")
        let fileURL = NSURL(fileURLWithPath: path!)
        do {
            player = try AVAudioPlayer(contentsOfURL: fileURL)
            player.prepareToPlay()
        } catch {
            print("Problem in getting File")
          }   
        player.play()


        sender.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal)

        //Store new
        currentlyPlaying = sender

        print("toggle state 1")

        if sender != currentlyPlaying{
            toggleState = 2
        }

        myTableView.reloadData()

    }
    else {
        player.pause()
        toggleState = 1
        sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
        print("Toggle state else")
}

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

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