使用Swift,我该如何制作:
答案 0 :(得分:4)
在故事板中,将默认状态标题设置为“开始”,将选中状态标题设置为“暂停”
或者,如果您在代码中执行此操作:
startPauseButton.setTitle("Start", forState: .Normal)
startPauseButton.setTitle("Pause", forState: .Selected)
在IBAction func
中,在button.selected
和true
之间切换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)
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; 显示助理编辑器
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; 显示助理编辑器