点击时如何反复更改按钮?

时间:2015-05-01 09:28:14

标签: ios xcode swift button

使用Swift,我该如何制作:

  1. 按钮,单词“start”,点击时会变为“暂停”,反之亦然?不只是一次,但可以无限地完成? (例如在秒表应用程序中)
  2. 自定义图像按钮,在点按时在2个图像之间切换,也可以无限制地完成(点击和更改)?

5 个答案:

答案 0 :(得分:4)

在故事板中,将默认状态标题设置为“开始”,将选中状态标题设置为“暂停”

enter image description here

enter image description here

或者,如果您在代码中执行此操作:

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

IBAction func中,在button.selectedtrue之间切换false属性。 @IBAction func toggleStopwatch(button:UIButton) { if button.selected { // Pause the stopwatch } else { // Start the stopwatch } button.selected = !button.selected }

</li>

您还可以为不同的状态设置不同的图像:默认突出显示已选择已禁用

答案 1 :(得分:3)

第1步:创建Bool变量

var isPlaying:Bool = false

第2步:使用此IBAction方法连接您的按钮:

@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
    }
  }

答案 2 :(得分:1)

在按钮的@IBAction中(假设您使用的是故事板),您可以将按钮的标题或图像更改为您想要的内容。您还可以检查标题或图像,并根据您拥有的内容执行操作。

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

或者使用图片(假设你有一个存储图像的images数组,你应该这样做):

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

答案 3 :(得分:0)

试试这个......

{{1}}

答案 4 :(得分:0)

#1。切换UIButton标题

使用Swift 3,UIButton有一个名为setTitle(_:for:)的方法。 setTitle(_:for:)有以下声明:

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

设置用于指定状态的标题。

以下Playground代码显示了如何使用setTitle(_:for:)根据其状态切换按钮的标题:

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's 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

使用查看&gt;在Playground助理编辑器中预览视图控制器助理编辑&gt; 显示助理编辑器

#2。切换UIButton图像

UIButton有一个名为setImage(_:for:)的方法。 setImage(_:for:)有以下声明:

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

设置用于指定状态的图像。

以下Playground代码显示了如何使用setImage(_:for:)根据其状态切换按钮的标题:

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's 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

使用查看&gt;在Playground助理编辑器中预览视图控制器助理编辑&gt; 显示助理编辑器