[英]How to change button repeatedly when tapped?
使用Swift,我該如何制作:
在故事板中,將默認狀態標題設置為“開始”,將選定狀態標題設置為“暫停”
或者,如果您在代碼中執行此操作:
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
}
您還可以為不同的狀態設置不同的圖像: 默認 , 突出顯示 , 選定 , 禁用
第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
}
}
在按鈕的@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(...)
}
嘗試這個....
@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
}
}
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
使用“ 視圖” >“ 助理編輯器” >“ 顯示助理編輯器”在Playground助手編輯器中預覽視圖控制器
UIButton
圖像 UIButton
有一個名為setImage(_:for:)
的方法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
使用“ 視圖” >“ 助理編輯器” >“ 顯示助理編輯器”在Playground助手編輯器中預覽視圖控制器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.