加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:53:51 所属栏目:百科 来源:网络整理
导读:我目前正在制作一个自定义的主题为NSButton。每个教程或指南,我发现需要对NSButtonCell进行子类化,甚至是 the guide from Apple。 所有这些似乎都是过时的,因为NSControl中的所有单元格方法都是deprecated in Yosemite.我没有发现任何建议或指导用作替代
我目前正在制作一个自定义的主题为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()
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读