为什么我的NSView会在点击时获得一些东西?

时间:2015-05-21 19:53:33

标签: swift cocoa nsview

在我制作的自定义NSView上,我用这种方式覆盖了mouseDown方法:

override func mouseDown(theEvent: NSEvent) {
let location = self.convertPoint(theEvent.locationInWindow, fromView: nil)
NSColor.blackColor().set()
let basisRect = CGRect(origin: location, size: CGSize(width: 10, height: 10))
let roundPath = NSBezierPath(ovalInRect: basisRect)
roundPath.fill()
needsDisplay = true 
}

但是当我点击时我没有画任何东西(即使我用println()检查时触发mouseDown方法)

你知道为什么没有发生任何事情以及如何解决它吗?

编辑(这是我的drawRect函数):

  override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        NSColor.blackColor().set()
        let middlePoint = (self.bounds.size.width/2)
        let basisRect = CGRect(origin: CGPoint(x: middlePoint-5, y: 10), size: CGSize(width: 10, height: 10))
        let roundPath = NSBezierPath(ovalInRect: basisRect)
        roundPath.fill()

        let bottomLegPath = NSBezierPath()
        bottomLegPath.moveToPoint(CGPoint(x: middlePoint, y: 10))
        bottomLegPath.lineToPoint(CGPoint(x: middlePoint, y: middlePoint))
        bottomLegPath.lineWidth = 5
        bottomLegPath.stroke()

        }

谢谢。

1 个答案:

答案 0 :(得分:4)

NSView通过调用drawRect:来绘制。只有drawRect:内容很重要。您没有显示drawRect:代码,但我打赌您没有。这是您需要绘制圆形路径的地方,而不是mouseDown实现。