我需要开发一个新的定制NSControl。我可以找到的所有指南和示例(包括苹果的NSControl子类文章)都是围绕NSCell构建的。但从10.10开始,NSControl上所有与单元格相关的消息都被否决了。
我尝试创建一个子类并通过IB中的自定义视图添加到我的项目中,但是我无法让控件接受第一个响应器,尽管启用了它,将refusesFirstResponder设置为NO,并从acceptsFirstResponder返回YES。我确信我缺少了很多功能(价值变更通知等等)。本来应该在那里的。
是否有新的参考资料显示现在应该如何开发控件?如果有我的Google-fu会让我失望的。谢谢!
发布于 2017-12-16 18:51:52
你的问题很可能是你从来没有把控制设置为第一个响应者。简单地点击它不会自动完成它。下面是一个快速的例子,它接受第一个响应者的状态(并变得如此点击也),并绘制一个焦点环。确保启用控件并设置其目标和操作。
class MyControl: NSControl {
override var acceptsFirstResponder: Bool {
return true
}
override func becomeFirstResponder() -> Bool {
return true
}
override func mouseDown(with event: NSEvent) {
window?.makeFirstResponder(self)
}
override func mouseUp(with event: NSEvent) {
if let action = action {
NSApp.sendAction(action, to: target, from: self)
}
}
override func draw(_ dirtyRect: NSRect) {
NSColor.white.set()
NSBezierPath(roundedRect: bounds.insetBy(dx: 1, dy: 1), xRadius: 3, yRadius: 3).fill()
if window?.firstResponder == self {
NSColor.keyboardFocusIndicatorColor.set()
} else {
NSColor.black.set()
}
NSBezierPath(roundedRect: bounds.insetBy(dx: 1, dy: 1), xRadius: 3, yRadius: 3).stroke()
}
override var focusRingMaskBounds: NSRect {
return bounds.insetBy(dx: 1, dy: 1)
}
override func drawFocusRingMask() {
NSBezierPath(roundedRect: bounds.insetBy(dx: 1, dy: 1), xRadius: 3, yRadius: 3).fill()
}
}https://stackoverflow.com/questions/27323132
复制相似问题