1

isHighlightedon 属性主要UIButton类似于按钮是否被按下。

Apple 文档对此含糊其辞:

控件会自动设置和清除此状态以响应适当的触摸事件。 https://developer.apple.com/documentation/uikit/uicontrol/1618231-ishighlighted

系统调用哪个方法来设置它?

它似乎是touchesBegan,因为您可以覆盖此方法来防止它。

但是,令人惊讶的是,如果您手动调用touchesBegan它并没有设置(例如,从超级视图中 - 参见下面的代码)。

所以看起来它并不简单touchesBegan。我已经尝试过覆盖所有我能做到的方法... pressesBegan, pressesChanged, pressesCancelled, pressesEndedtouchesCancelled, beginTracking, continueTracking, hitTest, 等等。

touchesBegan起到了一点作用。

以下是我尝试从超级视图调用 touchesBegan 的方法:

class MyView: UIView {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else { return }
        for view in self.subviews {
            if let button = view as? UIButton {
                if button.frame.contains(point) {
                    button.touchesBegan(touches, with: event)
                    print("\(button.isHighlighted)") //prints false
                }
            }
        }
    }
}
4

0 回答 0