osx – 如何创建一个自定义的主题NSButton,而不是NSButtonCell子
我目前正在制作一个自定义的主题为NSButton。每个教程或指南,我发现需要对NSButtonCell进行子类化,甚至是
the guide from Apple。
所有这些似乎都是过时的,因为NSControl中的所有单元格方法都是deprecated in Yosemite.我没有发现任何建议或指导用作替代物。 这是唯一的声明,我可以找到:
没有关于NSButton的话。 NSTextField支持层支持的视图;因为这样,我在我的NSButton上尝试了同样的方法,但是没有效果。 var btn = NSButton(NSMakeRect(0,50,20)) btn.wantsLayer = true btn.bordered = false btn.layer?.backgroundColor = NSColor(calibratedWhite: 0.99,alpha: 1).CGColor btn.layer?.borderWidth = 1 btn.layer?.borderColor = NSColor(calibratedWhite: 0.81,alpha: 1).CGColor
我一定会花更多时间研究层次支持的视图方法。我不知道为什么它不适合你,因为没有理由不能在NSButton上工作(实际上是NSView派生)。
更重要的是,您可以提到动画。 从我正在开发的项目中提取一些代码(自定义NSButton): 从init()… self.wantsLayer = true self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay self.layer?.borderColor = NSColor.gridColor().CGColor self.layer?.borderWidth = 0.5 self.layer?.backgroundColor = NSColor.whiteColor().CGColor 然后可以在特定于层的显示循环中获得细粒度控制: override var wantsUpdateLayer:Bool{ return true } override func updateLayer() { // your code here super.updateLayer() } 如果您的自定义按钮需要一个形状,那么您甚至可以使用下面的CAShapeLayer来使您的背衬层成为一个特殊的形状…更多的细节需要研究。 override func makeBackingLayer() -> CALayer { return CAShapeLayer() } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |