NSWindow在contentView

时间:2018-02-13 00:48:18

标签: swift cocoa drawing nsview nswindow

我正在以编程方式创建一个简单的窗口,我只需要在其内容视图中绘制一些行。线条所在区域的大小(worldBounds)需要按比例缩小,我假设我需要contentView的边界原点与worldBounds原点相同,因此它可以绘制所有内容。方法是将帧设置为大小的1/8,然后在contentView上使用 setBoundsOrigin setBoundsSize ,我理解缩放坐标系。

问题是没有绘图。窗口显示正确的大小。我有一个函数drawTestLines设置只是为了测试它。如果我删除对setBounds的调用,一切都很好,但当然边界与worldBounds不匹配。非常感谢任何帮助!

var worldBounds = NSRect()
let scale: CGFloat = 0.125

func drawMap() {

    boundLineStore(lineStore, &worldBounds)
    worldBounds.origin.x -= 8
    worldBounds.origin.y -= 8
    worldBounds.size.width += 16
    worldBounds.size.height += 16

    if !draw {
        return
    }

    let scaled = NSRect(x: 300.0,  // to set the window size/position
                        y: 80,
                        width: worldBounds.size.width*scale,   // 575
                        height: worldBounds.size.height*scale) // 355

    window = NSWindow(contentRect: scaled,
                      styleMask: .titled,
                      backing: .buffered,
                      defer: false)
    window.display()
    window.orderFront(nil)

    window.contentView!.setBoundsSize(worldBounds.size)     // (4593, 2833)
    window.contentView!.setBoundsOrigin(worldBounds.origin) // (-776, -4872)

    // Draw map lines
    window.contentView!.lockFocus()
    drawTestLines(in: window.contentView!)
    window.contentView!.unlockFocus()
}

// for testing
func drawTestLines(in view: NSView) {

    let bottom = view.bounds.minY
    let top = view.bounds.maxY
    let left = view.bounds.minX
    let right = view.bounds.maxX

    NSColor.black.setStroke()
    for i in Int(left)..<Int(right) { // draw vertical lines across the view just to see if it works!
        if i%64 == 0 {
            NSBezierPath.strokeLine(from: NSPoint(x: CGFloat(i), y: bottom), to: NSPoint(x: CGFloat(i), y: top))
        }
    }
    NSBezierPath.strokeLine(from: NSPoint(x: left, y: bottom), to: NSPoint(x: right, y: top))
    NSBezierPath.strokeLine(from: NSPoint.zero, to: NSPoint(x: 50.0, y: 50.0))
}

1 个答案:

答案 0 :(得分:0)

实验:创建一个新的Xcode项目。更改applicationDidFinishLaunching

func applicationDidFinishLaunching(_ aNotification: Notification) {
    if let contentBounds = window.contentView?.bounds {
        let scale: CGFloat = 0.5
        let worldBounds = CGRect(x: 0.0, y: 0.0, width: contentBounds.size.width * scale, height: contentBounds.size.height * scale)
        window.contentView!.bounds = worldBounds
    }
}

将IB中窗口的内容视图的类更改为MyView。添加一个新类MyViewNSView的子类。将draw更改为:

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)

    let bottom = self.bounds.minY
    let top = self.bounds.maxY
    let left = self.bounds.minX
    let right = self.bounds.maxX

    NSColor.black.setStroke()
    for i in Int(left)..<Int(right) { // draw vertical lines across the view just to see if it works!
        if i%64 == 0 {
            NSBezierPath.strokeLine(from: NSPoint(x: CGFloat(i), y: bottom), to: NSPoint(x: CGFloat(i), y: top))
        }
    }
    NSBezierPath.strokeLine(from: NSPoint(x: left, y: bottom), to: NSPoint(x: right, y: top))
    NSBezierPath.strokeLine(from: NSPoint.zero, to: NSPoint(x: 50.0, y: 50.0))
}

查看更改scaleworldBounds的来源时会发生什么。