How to change the button several times when pressed?

Using Swift, how do I do:

  • Button with the words "start", which changes to "pause" and vice versa when pressed? Not once, but can be done endlessly? (e.g. in a stopwatch application)
  • A custom image button that changes between 2 images when pressed, which can also be executed (knock and change) endlessly?
+6
source share
5 answers

Step 1: Create a Bool Variable

var isPlaying:Bool = false 

Step 2: Connect your button using 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 } } 
+3
source

In your storyboard, set the default status title to “Start” and the status title Selected to “Pause”

enter image description here

enter image description here

Or, conversely, if you do this in code:

 startPauseButton.setTitle("Start", forState: .Normal) startPauseButton.setTitle("Pause", forState: .Selected) 

In your IBAction func toggle the button.selected property between true and false

 @IBAction func toggleStopwatch(button:UIButton) { if button.selected { // Pause the stopwatch } else { // Start the stopwatch } button.selected = !button.selected } 

You can also set different images for different states: Default , Highlighted , Highlighted , Disabled

+4
source

In the @IBAction button (assuming you are using storyboards), you change the title or image of the button to whatever you want. You can also check the name or image and perform an action based on what you have.

 @IBAction func pressbutton(sender: UIButton) { switch sender.titleLabel?.text { case "Play": play() case "Pause": pause() default: other() } sender.titleLabel?.text = "Whatever you want" } 

Or with images (assuming you have an images array somewhere where the images you owe are stored):

 @IBAction func pressbutton(sender: UIButton) { switch sender.imageView!.image! { case images[0]: play() case images[1]: pause() default: other() } sender.imageView?.image = UIImage(...) } 
+1
source

Try it....

 @IBAction func btnStopWatch(sender: UIButton) { if btnStopWatch.selected == true { btnStopWatch.setTitle("stop", forState: UIControlState.Normal) btnStopWatch.setImage(stopImage, forState: UIControlState.Normal) btnStopWatch.selected = false } else { btnStopWatch.setTitle("start", forState: UIControlState.Normal) btnStopWatch.setImage(startImage, forState: UIControlState.Normal) btnStopWatch.selected = true } } 
0
source

# 1. Toggle UIButton

With Swift 3, UIButton has a setTitle(_:for:) method. setTitle(_:for:) has the following declaration:

 func setTitle(_ title: String?, for state: UIControlState) 

Sets the header to use in the specified state.

The following playground code shows how to use setTitle(_:for:) to switch the button title according to its state:

 import PlaygroundSupport import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.white let button = UIButton(type: UIButtonType.system) button.addTarget(self, action: #selector(toggle(sender:)), for: UIControlEvents.touchUpInside) // Set button states button.setTitle("Start", for: UIControlState.normal) button.setTitle("Pause", for: UIControlState.selected) // set layout view.addSubview(button) button.translatesAutoresizingMaskIntoConstraints = false let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor) let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint]) } /// trigger action when button is tapped func toggle(sender: UIButton) { sender.isSelected = !sender.isSelected print("Button state: \(sender.isSelected)") } } let vc = ViewController() PlaygroundPage.current.liveView = vc 

Preview the view controller in the playground assistant editor using View > Assistant Editor > Show Assistant Editor


# 2. Toggle UIButton Image

UIButton has a setImage(_:for:) method. setImage(_:for:) has the following declaration:

 func setImage(_ image: UIImage?, for state: UIControlState) 

Sets the image to use in the specified state.

The following pad code shows how to use setImage(_:for:) to toggle the button title according to its state:

 import PlaygroundSupport import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.white let button = UIButton() button.addTarget(self, action: #selector(toggle(sender:)), for: UIControlEvents.touchUpInside) // Set button states button.setImage(UIImage(named: "on.png"), for: UIControlState.normal) button.setImage(UIImage(named: "off.png"), for: UIControlState.selected) // set layout view.addSubview(button) button.translatesAutoresizingMaskIntoConstraints = false let horizontalConstraint = button.centerXAnchor.constraint(equalTo: view.centerXAnchor) let verticalConstraint = button.centerYAnchor.constraint(equalTo: view.centerYAnchor) let heightConstraint = button.heightAnchor.constraint(equalToConstant: 100) let widthConstraint = button.widthAnchor.constraint(equalToConstant: 100) NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, heightConstraint, widthConstraint]) } /// trigger action when button is tapped func toggle(sender: UIButton) { sender.isSelected = !sender.isSelected print("Button state: \(sender.isSelected)") } } let vc = ViewController() PlaygroundPage.current.liveView = vc 

Preview the view controller in the playground assistant editor using View > Assistant Editor > Show Assistant Editor

0
source

Source: https://habr.com/ru/post/986352/


All Articles