swift - 从 PaintCode 强制自定义 NSView 重绘
问题描述
我在PaintCode中创建了一个以编程方式绘制的图标(但这个问题不一定特定于该工具),我正试图让该图标重绘。
我使用这样的自定义类:
class IconTabGeneral: NSView {
override func draw(_ dirtyRect: NSRect) {
StyleKitMac.drawTabGeneral()
}
}
这drawTabGeneral()
是StyleKitMac
类中的一个方法(由 PaintCode 生成),如下所示(我将省略所有bezierPath
细节):
@objc dynamic public class func drawTabGeneral(frame targetFrame: NSRect = NSRect(x: 0, y: 0, width: 22, height: 22), resizing: ResizingBehavior = .aspectFit) {
//// General Declarations
let context = NSGraphicsContext.current!.cgContext
//// Resize to Target Frame
NSGraphicsContext.saveGraphicsState()
let resizedFrame: NSRect = resizing.apply(rect: NSRect(x: 0, y: 0, width: 22, height: 22), target: targetFrame)
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY)
context.scaleBy(x: resizedFrame.width / 22, y: resizedFrame.height / 22)
//// Bezier Drawing
let bezierPath = NSBezierPath()
...
bezierPath.close()
StyleKitMac.accentColor.setFill() ⬅️Custom color set here
bezierPath.fill()
NSGraphicsContext.restoreGraphicsState()
}
accentColor
定义有一个可以由用户更改的设置。在用户更改新颜色后,我无法让我的实例IconTabGeneral
重绘以获取新颜色。
我试过这个没有任何运气:
iconGeneralTabInstance.needsDisplay = true
我的理解是,这needsDisplay
将迫使该draw
功能再次触发,但显然不是。
知道如何让这个图标重绘并重新填充它bezierPath
吗?
解决方案
我想我明白了。事实证明,PaintCode 生成的StyleKitMac
类正在缓存颜色。事实上,图标是在needsDisplay
设置后重新绘制的。所以我只需要刷新缓存的颜色值。
推荐阅读
- python - 运行文件并运行选择为 Spyder 中的同一段代码提供不同的输出
- python - 如何将文件与在 Python 中引用它们的 xml 关联?
- python - “命令”对象没有属性“has_permissions”
- asp.net-core - 机密客户端应用程序缓存 - GetAccountAsync
- c# - 为什么我不能在 Visual Studio 中移动 Windows 窗体?
- python - 从 python 在符号链接目录中运行 Makefile
- c# - 转到行/循环切换语句?
- rust - Rust - 类型的值不能从 `std::iter::Iterator<>` 构建
- c++ - c ++:原子变量的值在保存在局部变量中时无效
- .net - .net 核心网络应用程序 docker 映像如何在 Mac OS 上运行?