鼠标按下行为NSControl和NSView的第一响应者 [英] First responder on mouse down behavior NSControl and NSView

查看:106
本文介绍了鼠标按下行为NSControl和NSView的第一响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom control. If it inherits from NSView, it automatically becomes the first responder when I click on it. If it inherits from NSControl, it does not. This difference in behavior persists, even if I override mouseDown(with:) and don't call 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 .我还检查了 refusesFirstResponder 属性.设置为false.

As you can see, I override acceptsFirstResponder among other methods and properties that are key view and responder related. I have also checked the refusesFirstResponder property. It is set to false.

  1. 这种行为差异的原因是什么?
  2. 是否存在可以重写以影响它的方法或属性?
  3. 说我想要一种行为,即单击该视图时该视图成为第一响应者,并且该视图从 NSControl 继承,在开始时调用 window!.makeFirstResponder(self)我的鼠标按下事件处理程序是一个好的解决方案,还是有更好的解决方案?
  1. What is the reason for this difference in behavior?
  2. Is there a method or property that I can override to influence it?
  3. Say I want the behavior where the view becomes the first responder when clicked and the view inherits from NSControl, is calling window!.makeFirstResponder(self) at the beginning of my mouse-down event handler a good solution or is there a better one?

推荐答案

要覆盖的属性是 needsPanelToBecomeKey .

布尔值,指示视图在处理键盘输入和导航之前是否需要其面板成为键窗口.

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

此属性的默认值为false.子类可以重写此属性,并使用其实现来确定视图是否要求其面板成为键窗口,以便它可以处理键盘输入和导航.这样的子类还应该重写acceptsFirstResponder以返回true.

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.

此属性还用于键盘导航.它确定鼠标单击是否应将焦点集中在视图上(即,使其成为第一响应者).某些视图(例如,文本字段)在单击时希望获得键盘焦点.其他视图(例如,按钮)仅在您按Tab键时才获得焦点.您不希望仅仅因为单击了复选框就将焦点从正在进行编辑的文本字段转移到了

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 返回 true NSControl 返回 false .

这篇关于鼠标按下行为NSControl和NSView的第一响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆