ios - 播放完成时快速更改按钮图像

标签 ios objective-c swift

当我点击播放按钮时发件人按钮改变但是当我点击表格 View 中的另一个按钮时按钮第一个按钮图像不会改变播放

 @IBAction func playSound(sender: UIButton) {
if self.player != nil && self.player.playing {
            player.stop()
            sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)            
        }
else {
sender.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal) 
}
}

enter image description here

最佳答案

**Try this code ...**  

 Declare one variable for get index.

 var songnamearray : NSArray = NSArray()
 var audioPlayer = AVAudioPlayer()
 var selectedindex : Int?
 var isFristtime : Bool = false



override func viewDidLoad() {
        super.viewDidLoad()

        songnamearray = ["Saiyaan (Sanam) 320Kbps(MajMasti.in)" ,"Phone Mein Teri Photo (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Mile Ho Tum Humko (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ," Rain Mashup (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Car Mein Music Baja (Neha Kakkar) 320Kbps"]

    }



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songnamearray.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Songcell", forIndexPath: indexPath)

      // info 5 play 6 name 7
        let eventname: UILabel = (cell.viewWithTag(7) as! UILabel)

        let info: UIButton = (cell.viewWithTag(5) as! UIButton)
         let play: UIButton = (cell.viewWithTag(6) as! UIButton)
        if selectedindex == indexPath.row{
            if isFristtime == true
            {
                play.setImage(UIImage(named: "pause-button.png"), forState: UIControlState.Normal)
            }else{
                play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
            }
        }else{
            play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
        }
        info.setImage(UIImage(named: "icon (5).png"), forState: UIControlState.Normal)
        play.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown)
        info.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown)
        eventname.text = songnamearray.objectAtIndex(indexPath.row) as? String

        return cell

    }



@IBAction func CloseMethod(sender: UIButton, event: AnyObject) {

        let touches = event.allTouches()!
        let touch = touches.first!
        let currentTouchPosition = touch.locationInView(self.Songlisttable)
        let indexPath = self.Songlisttable.indexPathForRowAtPoint(currentTouchPosition)!
        selectedindex = indexPath.row
        if isFristtime == true
        {
            audioPlayer.pause()
            isFristtime = false
        }else{
            let songnamestring :  NSString  = songnamearray.objectAtIndex(indexPath.row) as! String
            do {
                if let bundle = NSBundle.mainBundle().pathForResource(songnamestring as String, ofType: "mp3") {
                    let alertSound = NSURL(fileURLWithPath: bundle)
                    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                    try AVAudioSession.sharedInstance().setActive(true)
                    try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
                    audioPlayer.prepareToPlay()
                    audioPlayer.play()
                }
            } catch {
                print(error)
            }

            isFristtime = true
        }
        self.Songlisttable.reloadData()
    }

关于ios - 播放完成时快速更改按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848110/

相关文章:

iOS 动画 presentationLayer hitTest 似乎没有按预期工作

ios - 如何创建这样的弹跳弹出动画?

ios - 登录所需的环境沙盒问题

ios - iOS 9 中的 UISearchController - UISearchBar 中的 TextField 剪切出导航项

iphone - 在 iPhone VOIP 应用程序中保持 SIP 注册的时间间隔小于 600 秒

iphone - .mm 文件中的 DDMathParser

ios - Swift Decodable 无法使用单个根元素进行解码

ios - requestAccessToEntityType :completion: has been deprecated calling this is no longer allowed. 改为使用 requestFullAccessToEventsWithCompletion:

iphone - 执行 segue 后,MBProgressHUD 事件指示器不显示在 viewDidAppear 中

ios - 在 Objective C 中动态修改 UICollectionViewCell 中单元格的高度