osx – 如何创建一个自定义的主题NSButton,而不是NSButtonCell子类?

我目前正在制作一个自定义主题为NSButton。每个教程或指南,我发现需要对NSButtonCell进行子类化,甚至是 the guide from Apple

所有这些似乎都是过时的,因为NSControl中的所有单元格方法都是deprecated in Yosemite.我没有发现任何建议或指导用作替代物。

这是唯一的声明,我可以找到:

Gradual deprecation of NSCell

Mac OS X 10.10 takes another step towards the eventual deprecation of
cells. Direct access to the cell of a control is discouraged,and
methods which allow it will be formally deprecated in a subsequent
release. A variety of cell-level APIs have been promoted to varIoUs
Control subclasses in order to provide cell-free access to important
functionality. NSLevelIndicator,NSTextField,NSSearchField,NSSlider,
and NSPathControl all have new properties for this purpose. Cell-based
NSTableViews are Now deprecated,and view-based NSTableViews should be
used instead. Matrix-based NSbrowsers are also deprecated in favor of
the item-based interface.

Excerpt from: 07002

没有关于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()
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...