【问题标题】:Drawing on NSView在 NSView 上绘图
【发布时间】:2016-01-24 21:04:56
【问题描述】:

我正在尝试使用鼠标事件绘制视图,但 xcode 给了我“无效上下文”。谁能告诉我什么时候做错了?

class DrawingView: NSView {

var gContext:CGContextRef?

var lastPoint = NSPoint(x:0, y:0)
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false

func drawLineFrom(p1:NSPoint, p2:NSPoint){

    // CGContextBeginPath(gContext)
    // CGContextSetRGBStrokeColor(gContext, 0, 0, 0, 1)
    NSColor.redColor().set() // choose color
    CGContextSetLineWidth(gContext, 10.0)

    CGContextMoveToPoint(gContext, p1.x, p1.y)
    CGContextAddLineToPoint(gContext, p2.x, p2.y)

    CGContextStrokePath(gContext)

    // self.setNeedsDisplayInRect(self.bounds)
    needsDisplay = true

}

override func mouseUp(theEvent: NSEvent) {
    swiped = true

    let droppings = theEvent.locationInWindow
    let currentPoint:NSPoint = self.convertPoint(droppings, fromView: nil)
    self.drawLineFrom(lastPoint, p2: currentPoint)

    Swift.print("Up \(currentPoint)")
    lastPoint = currentPoint
}

override func mouseDown(theEvent: NSEvent) {
    // let position = theEvent.locationInWindow
    // Swift.print("Mouse Down: \(position.x) - \(position.y)")

    if theEvent.clickCount == 1{
        let point:NSPoint = self.convertPoint(theEvent.locationInWindow, fromView: self)
        Swift.print("Point >> \(point)")
        lastPoint = point
    }

}

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)

    // Drawing code here.
    let context = NSGraphicsContext.currentContext()?.CGContext

    let rawContext = NSGraphicsContext.currentContext()
    rawContext?.saveGraphicsState()

    self.gContext = context

    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
    CGContextSetLineWidth(context, 10.0)

    CGContextMoveToPoint(context, 0, 0)
    CGContextAddLineToPoint(context, 200, 200)

    CGContextStrokePath(context)
}

这是我的调试输出...

点>> (247.71484375, 108.34375) 10 月 25 日 15:40:07 OSX 基础知识 [17113]:CGContextSetFillColorWithColor:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。 10 月 25 日 15:40:07 OSX 基础知识 [17113]:CGContextSetStrokeColorWithColor:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。 向上(394.96484375、220.5078125)

【问题讨论】:

    标签: core-graphics swift2 xcode7 nsview cgcontext


    【解决方案1】:

    您不能在 draw rect 方法之外绘制视图的内容。错误是由于下面的代码:

    func drawLineFrom(p1:NSPoint, p2:NSPoint) {
        NSColor.redColor().set() // choose color
        // ....
    }
    

    您从鼠标向上事件调用 drawLineFrom(:)。当不存在上下文时,您尝试更改当前图形上下文的颜色。要重绘视图,您必须通过 self.needsDisplay = true 请求重绘,然后在 draw rect 方法中进行更改。

    这是一个示例代码:

    class DrawingView: NSView {
    
        var point: CGPoint = CGPoint.zero
    
        override func mouseUp(theEvent: NSEvent) {
            // Convert the point from the window coordinate to the view coordinate
            self.point = self.convertPoint(theEvent.locationInWindow, fromView: nil) 
            // We ask to redraw the view
            self.needsDisplay = true 
            Swift.print("Up \(self.point)")
        }
    }
    
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
    
        // You have to draw inside this function and not outside
        // The context is only valid here and you cannot retain it to draw later
        let context = NSGraphicsContext.currentContext()?.CGContext
        // Draw here
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2013-02-16
      • 1970-01-01
      相关资源
      最近更新 更多