gpt4 book ai didi

macos - 以编程方式快速清除 NSView

转载 作者:行者123 更新时间:2023-12-03 16:35:35 25 4
gpt4 key购买 nike

我有一个连接到自定义类的 NSView
该 View 上有一些绘图:

class LineDrawer: NSView {
var linear = NSBezierPath()
var storage = NSUserDefaults.standardUserDefaults()

override func drawRect(dirtyRect: NSRect) {
var color = NSColor()

if let newLoadedData = storage.objectForKey("color") as? NSData {
if let currentColor = NSUnarchiver.unarchiveObjectWithData(newLoadedData) as? NSColor {
color = currentColor
}
}

color.set()
linear.lineWidth = CGFloat(storage.integerForKey("width"))
linear.stroke()
}

override func mouseDown(theEvent: NSEvent) {
super.mouseDown(theEvent)
let location = theEvent.locationInWindow
var lastPt = theEvent.locationInWindow
lastPt.y -= frame.origin.y
linear.moveToPoint(lastPt)
}

override func mouseDragged(theEvent: NSEvent) {
super.mouseDragged(theEvent)
var newPt = theEvent.locationInWindow
newPt.y -= frame.origin.y
linear.lineToPoint(newPt)
needsDisplay = true
}
}

现在我需要编写一个函数来完全清除该 View 。

最佳答案

不太清楚完全清晰该 View 是什么意思,但你不能用纯色填充它吗?

[[NSColor windowBackgroundColor] setFill];
NSRectFill(self.bounds);

(尚未跟上 Swift 的热度)

更新我提高了我的 Swift 能力!

NSColor.windowBackgroundColor().setFill()
NSRectFill(self.bounds)

关于macos - 以编程方式快速清除 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com