UIButton
在理解 的概念时,我得到了一个奇怪的结果UIControlState
。这是我与UIButton
.
import UIKit
class ViewController: UIViewController {
let normalBtn: UIButton = {
let button = UIButton()
button.frame = CGRect(x: 80, y: 200, width: 200, height: 100)
button.setTitle("", for: .normal)
button.setTitle("", for: .highlighted)
button.setTitle("", for: .selected)
button.setTitle("", for: .focused)
button.titleLabel?.font = UIFont.systemFont(ofSize: 50)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(normalBtn)
normalBtn.addTarget(self, action: #selector(btnSelected), for: .touchUpInside)
}
@objc func btnSelected() {
print("highlight", normalBtn.isHighlighted)
normalBtn.isSelected = !normalBtn.isSelected
}
}
这是我关于此代码的方案。
- 当我触摸
normalBtn
时,此按钮的状态normal
变为selected
。 - 当我再次触摸
normalBtn
时,它的状态会从selected
变为normal
。 - 虽然这些转换,
highlighted
属性也应该改变,当我触摸normalBtn
.
所以我改变标题的期望是
- -> 触摸时 -> (
normal
toselected
) - -> 触摸时 -> (
selected
tonormal
)
但结果是,
- -> 触摸时 -> (
normal
toselected
) - ->(
selected
到normal
)
我真的不知道为什么。关于这个问题的任何想法?谢谢。