Overriding isHighlighted still changes UIControlState - why?

我只是一个虾纸丫 提交于 2019-12-11 15:55:45

问题


In a UIControl, if I override isHighlighted to set a private _isHighlighted property, and then check the control's state to see if it contains .highlighted, the state still accurately reflects the change. See code below.

My question is, how is this possible? I never call super.isHighlighted or manipulate the state property. state is an OptionSet that must have the .highlighted property inserted into the set, which, from what I can determine, does not, or should not, happen if I override the property.

The only other explanation I can think of is that state is actually a computed property based off other properties (i.e. isSelected, isEnabled, etc.)

class MyControl: UIControl {

    private var _isHighlighted: Bool = false

    override var isHighlighted: Bool {
        get { return self._isHighlighted }
        set { self._isHighlighted = newValue }
    }

}


let myControl = MyControl()
myControl.isHighlighted = true
myControl.state.contains(.highlighted) // returns true

回答1:


The only other explanation I can think of is that state is actually a computed property based off other properties (i.e. isSelected, isEnabled, etc.)

Good explanation! Let's try logging (printing) in the getter to see if that's true:

class MyControl: UIControl {
    private var _isHighlighted: Bool = false
    override var isHighlighted: Bool {
        get { print("getting"); return self._isHighlighted }
        set { self._isHighlighted = newValue }
    }
}
let myControl = MyControl()
myControl.isHighlighted = true
print("about to check state")
myControl.state.contains(.highlighted)
print("checked state")

And here's the log:

about to check state
getting
checked state

Quod erat demonstrandum.



来源:https://stackoverflow.com/questions/51828207/overriding-ishighlighted-still-changes-uicontrolstate-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!