我尝试在NSImageView上绘图,如果我想画一个椭圆形,它就可以了。如果我尝试绘制一条线,它就会失败。为什么? 我的对象声明:
class MyImgView: NSImageView {
var color = NSColor.greenColor()
var point1 = NSPoint()
var point2 = NSPoint()
init(frame: NSRect) {
super.init(frame: frame)
self.setNeedsDisplay()
self.point1 = NSPoint(x: 0, y: 0)
self.point2 = NSPoint(x: bounds.width, y:bounds.height)
}
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
//var path = NSBezierPath(ovalInRect: dirtyRect)
var path = NSBezierPath()
path.lineWidth = 10
path.moveToPoint(point1)
path.lineToPoint(point2)
color.setFill()
path.fill()
}
}
答案 0 :(得分:3)
因为path
没有区域,只有一条线。
color.setFill()
和path.fill()
应为color.setStroke()
和path.stroke()
:
class MyImgView: NSImageView {
var color = NSColor.greenColor()
var point1 = NSPoint()
var point2 = NSPoint()
override init(frame: NSRect) {
super.init(frame: frame)
self.setNeedsDisplay()
self.point1 = NSPoint(x: 0, y: 0)
self.point2 = NSPoint(x: bounds.width, y:bounds.height)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
//var path = NSBezierPath(ovalInRect: dirtyRect)
var path = NSBezierPath()
path.lineWidth = 10
path.moveToPoint(point1)
path.lineToPoint(point2)
color.setStroke() // <-- HERE
path.stroke() // <-- and HERE
}
}