gpt4 book ai didi

swift - 鼠标按下行为的第一响应者 NSControl 和 NSView

转载 作者:行者123 更新时间:2023-11-28 11:36:36 26 4
gpt4 key购买 nike

我有一个自定义控件。如果它继承自NSView,当我点击它时它会自动成为第一响应者。如果它继承自 NSControl,则不会。即使我重写 mouseDown(with:) 并且不调用 super,这种行为差异仍然存在。

代码:

class MyControl: NSView {
override var canBecomeKeyView: Bool { return true }
override var acceptsFirstResponder: Bool { return true }
override func drawFocusRingMask() { bounds.fill() }
override var focusRingMaskBounds: NSRect { return bounds }

override func draw(_ dirtyRect: NSRect) {
NSColor.white.set()
bounds.fill()
}
}

如您所见,我重写了 acceptsFirstResponder 以及与键 View 和响应程序相关的其他方法和属性。我还检查了 refusesFirstResponder 属性。它被设置为 false。

  1. 造成这种行为差异的原因是什么?
  2. 是否有我可以覆盖以影响它的方法或属性?
  3. 说我想要 View 在单击时成为第一响应者并且 View 继承自 NSControl 的行为,在开始时调用 window!.makeFirstResponder(self)我的鼠标按下事件处理程序是一个好的解决方案还是有更好的解决方案?

最佳答案

要覆盖的属性是 needsPanelToBecomeKey

A Boolean value indicating whether the view needs its panel to become the key window before it can handle keyboard input and navigation.

The default value of this property is false. Subclasses can override this property and use their implementation to determine if the view requires its panel to become the key window so that it can handle keyboard input and navigation. Such a subclass should also override acceptsFirstResponder to return true.

This property is also used in keyboard navigation. It determines if a mouse click should give focus to a view—that is, make it the first responder). Some views (for example, text fields) want to receive the keyboard focus when you click in them. Other views (for example, buttons) receive focus only when you tab to them. You wouldn't want focus to shift from a textfield that has editing in progress simply because you clicked on a check box.

NSView 返回trueNSControl 返回false

关于swift - 鼠标按下行为的第一响应者 NSControl 和 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55078226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com