Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

ios - change button image when play completes swift

sender button changes when i click play button but when i click another button in the tableview the button first button image not changes to play

 @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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
**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()
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...