Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change button repeatedly when tapped?

Using Swift, how do I make a:

  1. Button with words "start", that changes to "pause" and vice versa when tapped? Not just once, but can be done infinitely? (e.g. in a stopwatch app)
  2. Custom image button that changes between 2 image when tapped, that can also be done (tapped & change) infinitely?
like image 991
Allister Bah Avatar asked May 01 '15 09:05

Allister Bah


1 Answers

Step 1: Create a Bool variable

var isPlaying:Bool = false

Step 2: Connect your button with this IBAction method:

@IBAction func btnStartStop(sender: UIButton) {
    if isPlaying{

        isPlaying = false
        sender.setTitle("Pause", forState: UIControlState.Normal)
        sender.setImage(pauseImage, forState: .Normal)
        //Pause Stopwatch
    }
    else{

        isPlaying = true
        sender.setTitle("Play", forState: UIControlState.Normal)
        sender.setImage(playImage, forState: .Normal)
      //Play Stopwatch
    }
  }
like image 150
Mohammad Zaid Pathan Avatar answered Oct 24 '22 22:10

Mohammad Zaid Pathan