此问题是此主题的补充:tableview image content selection color
一切正常,但有一件事困扰我。 这是结果:
您可以看到图像的默认颜色为灰色。 如果选择一行,图像应该将颜色更改为白色 - 它有效!但你可以看到它会改变颜色之前的短暂延迟(我希望你看到我的意思)
我可以优化吗?
这是我的代码:
// Change Color of Image
func tintedImage(_ image: NSImage, tint: NSColor) -> NSImage {
guard let tinted = image.copy() as? NSImage else { return image }
tinted.lockFocus()
tint.set()
let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
imageRect.fill(using: .sourceAtop)
tinted.unlockFocus()
return tinted
}
func outlineViewSelectionIsChanging(_ notification: Notification) {
if lastSelectedRow != nil {
let view = myOutlineView.view(atColumn: 0, row: lastSelectedRow!, makeIfNecessary: true) as! CustomCell
view.imgView.image = self.tintedImage(NSImage(named: NSImage.Name(rawValue: "MyImage"))!, tint: NSColor.grey)
}
lastSelectedRow = myOutlineView.selectedRow
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: self) as? CustomCell
if lastSelectedRow == outlineView.selectedRow {
view?.imgView.image = self.tintedImage(NSImage(named: NSImage.Name(rawValue: "MyImage"))!, tint: NSColor.white)
} else {
view?.imgView.image = self.tintedImage(NSImage(named: NSImage.Name(rawValue: "MyImage"))!, tint: NSColor.grey)
}
return view
}