提问人:sajjad ahmad 提问时间:5/26/2019 最后编辑:Hichem BOUSSETTAsajjad ahmad 更新时间:5/26/2019 访问量:895
单击另一个按钮时更改声音按钮图像 swift 2.2
Change sound button image on clicking another button swift 2.2
问:
我正在创建一个 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")
}
答:
1赞
Mihir Luthra
5/26/2019
#1
在 u 类中,声明一个变量,
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")
}
评论
0赞
Bhargav Rao
5/26/2019
评论不用于扩展讨论;此对话已移至 Chat。
0赞
sajjad ahmad
5/26/2019
再次单击同一按钮不会停止声音,也不会将图像更改为暂停。我尝试了编辑后的代码
评论