OSX CustomView 在 Window 调整大小后不起作用

OSX CustomView Doesn't Work After Window Resize

我有一个显示 NSBezierpath 的简单视图。在路径内的 mouseDown 上,路径的填充颜色设置为黄色并且视图重绘。在路径外的 mouseDown 上,路径的填充颜色设置为蓝色并且视图重绘。

在我的情节提要中,我有一个 window 控制器,其中 window 内容转至视图控制器。视图,customview class HeartView(下图)填满了整个视图控制器。

在用户垂直调整 window 之前一切正常。在那之后,视图表现出奇怪的行为:mouseDown 不再在路径内的任何地方起作用,重新着色有时发生在路径外的 mouseDown 上,并且路径有时(但不总是)没有完全填满。我认为超级视图中发生了一些事情,但我不知道是什么。

    import Cocoa

    class HeartView: NSView {

        var mouseLocation : NSPoint = NSZeroPoint

        func drawObject(){
            //Create an empty Bezier path
            let aBezier : NSBezierPath = NSBezierPath()
            aBezier.moveToPoint(CGPoint(x: 176.95,y: 44.90))
            aBezier.curveToPoint(CGPoint(x: 166.71,y: 145.89),
                controlPoint1: CGPoint(x: 76.63,y: 76.78),
                controlPoint2: CGPoint(x: 82.59,y: 206.70))
            aBezier.curveToPoint(CGPoint(x: 176.95,y: 44.90),
                controlPoint1: CGPoint(x: 237.55,y: 224.76),
                controlPoint2: CGPoint(x: 276.83,y: 95.98))
            aBezier.closePath()
                    if (aBezier.containsPoint(NSMakePoint(mouseLocation.x, mouseLocation.y))){
                        NSColor.yellowColor().setFill()
                        NSColor.greenColor().setStroke()        
                    } else {
                        NSColor.blueColor().setFill()
                        NSColor.orangeColor().setStroke()
                    }
                    aBezier.fill()
                    aBezier.lineWidth = 2.0
                    aBezier.stroke()
         }

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

        override func mouseDown(theEvent: NSEvent) {
            mouseLocation.x = theEvent.locationInWindow.x
            mouseLocation.y = theEvent.locationInWindow.y
            self.setNeedsDisplayInRect(self.frame)  
        }

}

我在 Lucas Derraugh 关于鼠标事件的视频(Cocoa 编程 L27)中找到了答案。事实证明,我在父视图的坐标系中捕获了 mouseDown 事件。在 mouseDown 事件中,我使用了 "locationInWindow,",这是导致奇怪行为的原因。我将方法更改为:

    override func mouseDown(theEvent: NSEvent) {
         var viewPoint:NSPoint = self.convertPoint(theEvent.locationInWindow, fromView: nil)
         mouseLocation.x = viewPoint.x
         mouseLocation.y = viewPoint.y
         self.needsDisplay = true
} 

从 window 的坐标系转换到视图的坐标系。在任何 window 调整大小事件后,现在一切正常。