简体   繁体   中英

How do I change the image of a button when tapped - Swift

I'm trying to change the image of a button when tapped (it's for an audio player that I want Play to change to Pause). So far I've only managed to change it when the button is held, then it changes back when released.

I've tried using Selected instead of Highlighted but that doesn't work. (The initial image is set in the Attributes Inspector). This is what I have -

 @IBAction func buttonTapped(sender: AnyObject) {
           ColourTestButton.setImage(UIImage(named: "blank-purple.jpg"), forState:.Highlighted) 
        }

I realise Highlighted means just that though on another image where I wanted it to change back (in a Cell of a Table View) it stayed, which is why I thought it would work for this one. What do I have to do here to make it stay changed to the new image?

I think you need this code:

@IBAction func buttonTapped(sender: AnyObject) {
       let image = UIImage(named: "blank-purple.jpg") as UIImage!
       let playButton  = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
       ColourTestButton.setImage(image, forState: .Normal)
}

UPDATE:

If you want to change the image like play/pause then you can do it this way:

var pressed = false
@IBAction func pressed(sender: AnyObject) {

    if !pressed {
        let image = UIImage(named: "pauseImage.png") as UIImage!
        let playButton  = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        btn.setImage(image, forState: .Normal)
        pressed = true
    } else {

        let image = UIImage(named: "playImage.png") as UIImage!
        let playButton  = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        btn.setImage(image, forState: .Normal)
        pressed = false
    }

}

I think you are updating the image only at Highlighted state, so when you release the button it goes back to an original image. Try to change it at Normal state so that it will change the image. Take one flag to check the Play and Pause condition.

@IBAction func buttonTapped(sender: AnyObject) {
       if (!flag)
       { 
         //for Play
         flag = true
         ColourTestButton.setImage(UIImage(named:"USE PLAY IMAGE"), forState:. Normal) 
       }
       else
       {
         //for Pause
         flag = false
         ColourTestButton.setImage(UIImage(named:"USE PAUSE IMAGE"), forState:. Normal)
       }
    }

Though Dharmesh Kheni's answer is OK I would recommend to use two buttons. You can set them in separate function or in interface builder and then just show and hide them according to state. Something like this:

func setButtons()
{
      playBtn.setImage(UIImage(named: "play.png"),  forState:.Normal)
      pauseBtn.setImage(UIImage(named: "play.png"),  forState:.Normal)
      pauseBtn.hidden = true
}

@IBAction func playTapped(sender: AnyObject) 
{
      playBtn.hidden = true
      pauseBtn.hidden = flase

      //do other stuff 
}

@IBAction func pauseTapped(sender: AnyObject) 
{
      playBtn.hidden = false
      pauseBtn.hidden = true

      //do other stuff 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM