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

Change sound button image on clicking another button swift 2.2

我正在创建一个 Tableview,里面是 Tableviewcell,在单元格上有一个标签和声音按钮。对于每个标签,单击按钮时都会发出声音。当我第一次点击 btn1 时,声音播放并且按钮图像变为 "pause" 当我再次单击时相同的按钮声音停止并且图像变为 "play" 以这种方式完美地工作但是当我第一次单击一个按钮让假设 btn1 并且不再单击它(停止声音)我单击 btn2,btn1 的声音停止并且 btn1 和 btn2 的图像发生变化。我希望当我点击 btn 2,3 或 4 时,之前的声音应该停止,前一个按钮的图像(表示除当前按钮之外的所有按钮)应该更改为 "play" 并且当前单击的按钮应该更改为 "pause" 并且之前点击的声音应该停止并且当前点击的应该播放。

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

在你class声明一个变量,

var currentlyPlaying : UIButton?

无论你在哪里播放声音,将播放声音的按钮存储在这个变量中,每当这个变量将要改变时,重置以前的图像并存储新的图像。

@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")
}