创建一个 if 语句,其中一次只有一个按钮可以有边框
create a if statement where only one button at a time can have a border
我希望我的 swift 代码使用 if 语句或其他序列,以便在一次单击时仅在其中一个按钮上显示边框。因此,一次只能在一个按钮上看到边框,该按钮将是最后一个按下的按钮。我知道我可以说 layer.border 每个按钮上应该选择 0 但我想看看是否有更有效的方法来做到这一点。
import UIKit
class ViewController: UIViewController {
var ba = UIButton()
var bb = UIButton()
var bc = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[ba,bb,bc].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
ba.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
bb.frame = CGRect(x: 100, y: 0, width: 100, height: 100)
bc.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
ba.backgroundColor = .blue
bb.backgroundColor = .orange
bc.backgroundColor = .darkGray
ba.addTarget(self, action: #selector(pressa), for: .touchDown)
bb.addTarget(self, action: #selector(pressb), for: .touchDown)
bc.addTarget(self, action: #selector(pressc), for: .touchDown)
}
@objc func pressa(){
ba.layer.borderWidth = 2
}
@objc func pressb(){
bb.layer.borderWidth = 2
}
@objc func pressc(){
bc.layer.borderWidth = 2
}
}
可能是这样的一种方法
[ba,bb,bc].forEach { [=10=].addTarget(self, action: #selector(pressAll(_:)), for: .touchDown) }
}
@objc func pressAll(_ sender:UIButton) {
[ba,bb,bc].forEach { [=10=].layer.borderWidth = 0 } // reset all
sender.layer.borderWidth = 2
}
您可以将目标添加到 forEach 中的所有按钮,并且只有一种方法如@Sh_Khan 提及
import UIKit
class ViewController: UIViewController {
var ba = UIButton()
var bb = UIButton()
var bc = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[ba,bb,bc].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].addTarget(self, action: #selector(pressAll(_:)), for: .touchDown)
}
ba.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
bb.frame = CGRect(x: 100, y: 0, width: 100, height: 100)
bc.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
ba.backgroundColor = .blue
bb.backgroundColor = .orange
bc.backgroundColor = .darkGray
}
@objc func pressAll(_ sender:UIButton) {
[ba,bb,bc].forEach { [=10=].layer.borderWidth = 0 } // reset all
sender.layer.borderWidth = 2
}
}
看看您是如何在 [ba,bb,bc].forEach { ... }
中使用数组来减少代码重复的?使用数组是关键。不要像那样将三个按钮放在一个内联数组中,而是创建一个 属性:
var buttons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
buttons = [ba,bb,bc]
buttons.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].addTarget(self, action: #selector(buttonPressed), for: .touchDown)
}
...
}
我对所有三个按钮使用了相同的选择器 buttonPressed
。 buttonPressed
可以接受类型为 UIButton
的参数,它告诉我们按下了哪个按钮:
@objc func buttonPressed(_ sender: UIButton) {
buttons.forEach { ba.layer.borderWidth = 0 } // deselect all buttons first...
sender.layer.borderWidth = 2 // select the tapped button
}
如果您要管理的按钮超过 3 个,我建议您根本不要使用 UIButton
。您应该使用 UICollectionView
. (Learn how to use them) 此视图将为您处理选择。当 space 不足以显示所有按钮时,它还允许滚动。您只需要创建一个自定义 UICollectionViewCell
并覆盖其 isSelected
属性:
override var isSelected: Bool {
didSet {
if isSelected {
self.layer.borderWidth = 2
} else {
self.layer.borderWidth = 0
}
}
}
我希望我的 swift 代码使用 if 语句或其他序列,以便在一次单击时仅在其中一个按钮上显示边框。因此,一次只能在一个按钮上看到边框,该按钮将是最后一个按下的按钮。我知道我可以说 layer.border 每个按钮上应该选择 0 但我想看看是否有更有效的方法来做到这一点。
import UIKit
class ViewController: UIViewController {
var ba = UIButton()
var bb = UIButton()
var bc = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[ba,bb,bc].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
ba.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
bb.frame = CGRect(x: 100, y: 0, width: 100, height: 100)
bc.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
ba.backgroundColor = .blue
bb.backgroundColor = .orange
bc.backgroundColor = .darkGray
ba.addTarget(self, action: #selector(pressa), for: .touchDown)
bb.addTarget(self, action: #selector(pressb), for: .touchDown)
bc.addTarget(self, action: #selector(pressc), for: .touchDown)
}
@objc func pressa(){
ba.layer.borderWidth = 2
}
@objc func pressb(){
bb.layer.borderWidth = 2
}
@objc func pressc(){
bc.layer.borderWidth = 2
}
}
可能是这样的一种方法
[ba,bb,bc].forEach { [=10=].addTarget(self, action: #selector(pressAll(_:)), for: .touchDown) }
}
@objc func pressAll(_ sender:UIButton) {
[ba,bb,bc].forEach { [=10=].layer.borderWidth = 0 } // reset all
sender.layer.borderWidth = 2
}
您可以将目标添加到 forEach 中的所有按钮,并且只有一种方法如@Sh_Khan 提及
import UIKit
class ViewController: UIViewController {
var ba = UIButton()
var bb = UIButton()
var bc = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[ba,bb,bc].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].addTarget(self, action: #selector(pressAll(_:)), for: .touchDown)
}
ba.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
bb.frame = CGRect(x: 100, y: 0, width: 100, height: 100)
bc.frame = CGRect(x: 200, y: 0, width: 100, height: 100)
ba.backgroundColor = .blue
bb.backgroundColor = .orange
bc.backgroundColor = .darkGray
}
@objc func pressAll(_ sender:UIButton) {
[ba,bb,bc].forEach { [=10=].layer.borderWidth = 0 } // reset all
sender.layer.borderWidth = 2
}
}
看看您是如何在 [ba,bb,bc].forEach { ... }
中使用数组来减少代码重复的?使用数组是关键。不要像那样将三个按钮放在一个内联数组中,而是创建一个 属性:
var buttons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
buttons = [ba,bb,bc]
buttons.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
[=10=].addTarget(self, action: #selector(buttonPressed), for: .touchDown)
}
...
}
我对所有三个按钮使用了相同的选择器 buttonPressed
。 buttonPressed
可以接受类型为 UIButton
的参数,它告诉我们按下了哪个按钮:
@objc func buttonPressed(_ sender: UIButton) {
buttons.forEach { ba.layer.borderWidth = 0 } // deselect all buttons first...
sender.layer.borderWidth = 2 // select the tapped button
}
如果您要管理的按钮超过 3 个,我建议您根本不要使用 UIButton
。您应该使用 UICollectionView
. (Learn how to use them) 此视图将为您处理选择。当 space 不足以显示所有按钮时,它还允许滚动。您只需要创建一个自定义 UICollectionViewCell
并覆盖其 isSelected
属性:
override var isSelected: Bool {
didSet {
if isSelected {
self.layer.borderWidth = 2
} else {
self.layer.borderWidth = 0
}
}
}