确定NSBezierPath范围以确保在macOS上正确重画的正确方法是什么

时间:2018-08-13 00:30:19

标签: macos nsbezierpath

我在确定正确绘制NZBezierPath边界的正确方法时遇到了麻烦。

从下面的图片中可以看到,细边框线似乎落在NSBezierPath.bounds矩形之外。

用于处理对象拖动的代码如下:

func offsetItemLocationBy(item: DItem, x: CGFloat, y:CGFloat)
    {
        // tell the display to redraw the old rect
        let oldBounds = item.bounds  // NSBezierPath.bounds

        // since the offset can be generated by both mouse moves
        // and moveUp:, moveDown:, etc.. actions, we'll invert
        // the deltaY amount based on if the view is flipped or
        // not.
        let invertDeltaY: CGFloat = self.isFlipped ? -1.0: 1.0
        let y1=y*invertDeltaY

        item.offsetLocationBy(x: x, y: y1) // Creates a new NSBezierPath

        self.setNeedsDisplay(oldBounds)
        // invalidate the new rect location so that it'll
        // be redrawn
        self.setNeedsDisplay(item.bounds)

    }

我应该使用其他方法来计算NSBezierPath的边界吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,我刚刚弄清楚-NSBezierPath.bounds不包含行宽。

因此,要计算正确的边界,请从lineWidth/2.0x中减去y,然后将lineWidth添加到widthheight中。

var bounds: NSRect {
        let b = path.bounds
        let sw = path.lineWidth/2.0
        let boundsRect = NSRect(x: b.minX-sw, y: b.minY-sw, width: b.width+2*sw, height: b.height+2*sw)
        return boundsRect
    }