Bri*_*kel 60
与其他提到的一样,您可以使用它willSet来检测变化 但是,在覆盖中,您不需要将值分配给super,而只是观察现有的更改.
您可以从以下游乐场观察几件事:
willSet/didSet仍然调用super 的属性get/set.你可以说,因为状态从.normal变为.selected.selected无论是newValue在willSet或oldValue在didSet确定是否要动画.import UIKit
class MyButton : UIButton {
override var isSelected: Bool {
willSet {
print("changing from \(isSelected) to \(newValue)")
}
didSet {
print("changed from \(oldValue) to \(isSelected)")
}
}
}
let button = MyButton()
button.state == .normal
button.isSelected = true // Both events fire on change.
button.state == .selected
button.isSelected = true // Both events still fire.
Run Code Online (Sandbox Code Playgroud)
你会像这样做:
class MyButton : UIButton {
// ...
override var isSelected: Bool {
willSet(newValue) {
super.isSelected = newValue;
// do your own business here...
}
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28458 次 |
| 最近记录: |