更改单击其他按钮时的声音按钮图像swift 2.2

x6yk4ghg  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(194)

我正在创建一个Tableview,其中有Tableview单元格,单元格上有一个标签和声音按钮。对于每个标签,单击按钮时都有一个声音。当我第一次单击btn1时,声音播放,按钮图像变为“暂停”。当我再次单击同一按钮时,声音停止,图像变为“播放”。以这种方式完美地工作,但当我第一次点击一个按钮,让假设btn1,并没有再次点击它(停止声音)我点击btn2,btn1的声音停止,btn1和btn2的图像也不改变。我希望当我点击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

mbskvtky

mbskvtky1#

在类中声明一个变量,
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")
}

相关问题