gpt4 book ai didi

swift - 删除填充的 NSBezierPath

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:08 26 4
gpt4 key购买 nike

我画了一个红色圆圈。然后我想删除它。我该怎么做?

class Red: NSView {
var red = 255
var green = 0
var blue = 0

override func draw(_ dirtyRect: NSRect) {
let circleFillColor = NSColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)
let cPath: NSBezierPath = NSBezierPath(ovalIn: dirtyRect)
circleFillColor.set()
cPath.fill()
}
}

override func viewDidLoad() {
super.viewDidLoad()
let signal = Red(frame: NSRect(x: 146, y: 18, width: 25, height: 25))
self.view.addSubview(signal)
}

最佳答案

您可以使用 View 的 removeFromSuperview 方法来获取是否已删除,如下所示:

class ViewController: NSViewController {

var signal: Red?

override func viewDidLoad() {
super.viewDidLoad()

self.signal = Red(frame: NSRect(x: 146, y: 18, width: 25, height: 25))
self.view.addSubview(signal!)
}

func deleteCircle() {
self.signal?.removeFromSuperview()
self.signal = nil
}

}

关于swift - 删除填充的 NSBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088453/

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