如何使用Swift游乐场显示带有某些图形的NSView?

如何使用Swift游乐场显示带有某些图形的NSView?,swift,nsview,swift-playground,Swift,Nsview,Swift Playground,基本上,我想在NSView的Swift游乐场中测试图表绘制 以下是我现在使用的: class CustomView: NSView { init(frame: NSRect) { super.init(frame: frame) } override func drawRect(dirtyRect: NSRect) { color.setFill() NSRectFill(self.bounds) NSColor.yellowColor().setFi

基本上,我想在NSView的Swift游乐场中测试图表绘制

以下是我现在使用的:

class CustomView: NSView {
  init(frame: NSRect) {
    super.init(frame: frame)
  }
  override func drawRect(dirtyRect: NSRect) {
    color.setFill()
    NSRectFill(self.bounds)
    NSColor.yellowColor().setFill()
    var r = NSMakeRect(0, y, self.bounds.width, 5)
    NSRectFill(r)
  }

  var color = NSColor.greenColor()
  var y: CGFloat = 0
}
var view = CustomView(frame:
  NSRect(x: 0, y: 0, width: 300, height: 300))

view.color = NSColor.greenColor()
XCPShowView("chart", view)

for var i = 0; i < 100; ++i {
  view.y = CGFloat(i) // <- try click on a plus sign opposite this line to add it to timeline
}
类自定义视图:NSView{
初始化(帧:NSRect){
super.init(frame:frame)
}
重写函数drawRect(dirtyRect:NSRect){
color.setFill()
NSRectFill(self.bounds)
NSColor.yellowColor().setFill()
var r=NSMakeRect(0,y,self.bounds.width,5)
NSRectFill(r)
}
var color=NSColor.greenColor()
变量y:CGFloat=0
}
变量视图=自定义视图(框架:
NSRect(x:0,y:0,宽:300,高:300))
view.color=NSColor.greenColor()
XCPShowView(“图表”,视图)
对于var i=0;i<100++我{
view.y=CGFloat(i)/更神奇一点:

var y: CGFloat = 0 {
    willSet(y) {
}
    didSet {
        self.display()
    }
}

在您的xcode swift游乐场环境中尝试这段代码,确保将所有对象初始化都放在init中,因为draw会执行多次:-),这只是一个简单的示例,后面还有更多

import Cocoa
import XCPlayground


  class CustomView: NSView {
    init(frame: NSRect) {
        super.init(frame: frame)

        antibez.moveToPoint(NSPoint(x: 10 , y: 10))
        for i in 0..25
        {
            antibez.lineToPoint(NSPoint(x: 20 + 10 * (25-i), y: 20 + 10 * i))
            antibez.moveToPoint(NSPoint(x: 10 + 10 * (i), y: 10 ))

        }
    }
    override func drawRect(dirtyRect: NSRect) {
        color.setFill()
        NSRectFill(self.bounds)
        antibez.stroke()

    }

    var color = NSColor.greenColor()

    var antibez = NSBezierPath()
  }
  var view = CustomView(frame:
    NSRect(x: 0, y: 0, width: 300, height: 300))

  XCPShowView("chart", view)

检查这段代码,它在Turtles中滑动(请注意PNG文件的位置,您可能需要将其调整到您自己的位置/文件)


这是Xcode 9.3上Swift 4.1的答案:

//: Playground - noun: a place where people can play

import Cocoa
import PlaygroundSupport

class CustomView: NSView {
    override func draw(_ dirtyRect: NSRect) {
        // Add your drawing code here.
        let backgroundColor = NSColor.red
        backgroundColor.set()
        NSBezierPath.fill(bounds)
    }
}


let customView = CustomView(frame: NSRect(x: 0, y: 0, width: 400, height: 400))

PlaygroundPage.current.liveView = customView
要显示liveView,请确保通过在游乐场工具栏中选择带有两个互锁圆圈的按钮或通过菜单显示助理编辑器:
视图>助手编辑器>显示助手
编辑器

你怎么能在这里导入XCPlayde?奇怪的是,它还不允许我这么做。我下载了Xcode beta6。打开它,在新项目中选择“开始游戏”,检查教程中关于此问题的第一个屏幕截图,然后您可以导入XCPlaygroundXCPShowView现在已弃用。使用
XCPlaygroundPage.currentPage.liveview=view
而不是Xcode 8+导入
PlaygroundSupport
上的
XCPShowView
并使用:
PlaygroundPage.current.liveview=view
。我也@Jeff no it不是-此问题在NSView中提出,您所指的问题是UIView,因此适用于不同的操作系统
//: Playground - noun: a place where people can play

import Cocoa
import PlaygroundSupport

class CustomView: NSView {
    override func draw(_ dirtyRect: NSRect) {
        // Add your drawing code here.
        let backgroundColor = NSColor.red
        backgroundColor.set()
        NSBezierPath.fill(bounds)
    }
}


let customView = CustomView(frame: NSRect(x: 0, y: 0, width: 400, height: 400))

PlaygroundPage.current.liveView = customView