我正在创建一个Tableview,里面是Tableviewcell,在单元格上有一个标签和声音按钮。对于每个标签,单击按钮时都会发出声音。当我第一次在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")
}
答案 0 :(得分:1)
在您的课程中,声明一个变量,
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")
}