2016-08-31 32 views
1

我想使用此代碼繪製在NSImageView鉛筆條紋畫在OSX鉛筆條紋:使用SWIFT

class DrawView: NSImageView 
{ 
    var point = NSPoint() 
    override func drawRect(dirtyRect: NSRect) 
    { 
    let myContext = NSGraphicsContext.currentContext()?.CGContext 
    CGContextSetRGBStrokeColor (myContext, 1, 0, 0, 1) 
    CGContextAddArc(myContext, point.x, point.y,1,0.1,0,0) 
    CGContextSetLineWidth(myContext, 1) 
    CGContextStrokePath(myContext) 
    } 

    func MousePan(mouseLocation: NSPoint) 
    { 
    point = mouseLocation 
    self.setNeedsDisplay() 
    } 
} 

到目前爲止,幾乎所有的作品很好,我得到我的MousePointer一個紅色的點作爲只要我點擊我的視圖移動鼠標。 我的問題是,我沒有看到我的觀點的條紋,只是這個小紅點。 我必須做些什麼才能讓這件事情起作用?

在此先感謝。

回答

0

首先,您應該使用NSView的子類,而不是NSImageView

而且這些方法有點兒缺乏。

它去更像是:

  • CGContextMoveToPoint
  • 然後CGContextLineToPointCGContextCurvedLineToPoint
  • 然後如果有必要CGContextClosePath
  • 然後CGContextStrokePath