【问题标题】:How to fix CoreGraphics dashed line changing when view is resized调整视图大小时如何修复CoreGraphics虚线更改
【发布时间】:2019-04-22 06:41:45
【问题描述】:

我有一个 NSView 的子类,它正在绘制一系列虚线。当我调整视图所在的窗口大小时,虚线会改变它们的模式。如何防止线条在调整窗口大小时改变它们的图案?

正在以下函数中绘制线条:

func drawLines(in context: CGContext) {
    for line in lines {
        context.move(to: line.start)
        context.addLine(to: line.end)
        context.setLineDash(phase: 0, lengths: [10, 10])
        context.strokePath()
    }
}

这个函数是从draw(_:)中调用的:

override func draw(_ dirtyRect: NSRect) {
    guard let context = NSGraphicsContext.current?.cgContext else { return }

    drawLines(in: context)
}

调整窗口大小时,线条图案会发生变化 - 有时虚线看起来完全是实线:

【问题讨论】:

    标签: swift macos core-graphics


    【解决方案1】:

    事实证明,我的lines 数组包含每条线的两个方向的线。这意味着对于每条线,它在两个方向上都画了一条虚线。这些虚线重叠,在调整大小时会出现同相和异相,从而导致这种行为。

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多