在NSImage上绘制NSBezierPath未正确更新

时间:2015-10-15 01:41:00

标签: swift cocoa statusbar nsimage nsbezierpath

我有一个NSStatusBar项目我正在绘制(根据用户设置的变量)图像。设置图像后,我在图像上绘制一个简单的矩形/ bezier路径以指示某种进度。 progress / rect似乎没有更新,但是当我更改填充的颜色时,您可以看到它正确绘制,但是原始颜色的“progress rect”仍然存在于其后面。这就是我所拥有的:

button.image = nil  // resetting this seems like it would also clear it out?
var img : NSImage
let perc = Int(cPerc)

if round == true {
    img = NSImage(named: "round")!
} else {
    img = NSImage(named: "square")!
}

button.image = img

let frame = CGRect(x: 0, y: 0, width: 25, height: 15)   // main button icon frame

drawInnerRect(frame: frame, cperc: self.curPerc, attr: atr) // draw the inside bezierpath
button.needsDisplay = true  // should update the drawing???
...


func drawInnerRect(frame frame: NSRect, cperc : NSNumber, attr: NSDictionary)
{
    let fillColor = attr.valueForKey("fill") as! NSColor

    statusItem.button!.image?.lockFocus()

        let per = ((cperc.doubleValue / 100.0) * 17.0)
        let rectanglePath = NSBezierPath(rect: NSMakeRect(frame.minX+2, frame.minY+2, CGFloat(per), 8))

        NSColor.clearColor().set()  // trying to use this *should* reset the rect fill??
        rectanglePath.fill()

        fillColor.setFill()
        rectanglePath.fill()

    statusItem.button!.image?.unlockFocus()
}

这是我在这里的第一篇文章,温柔......

0 个答案:

没有答案