如何使用Swift playground来显示带有绘图的NSView?

时间:2014-06-04 18:51:58

标签: swift nsview swift-playground

基本上我想在NSView的Swift游乐场中测试一个图表绘图。

5 个答案:

答案 0 :(得分:8)

以下是我现在使用的内容:

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
}

答案 1 :(得分:4)

在xcode swift playground环境中尝试此代码,请务必将所有对象初始化放在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)

答案 2 :(得分:2)

甚至更神奇:

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

答案 3 :(得分:1)

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

// Playground - noun: a place where people can play
// Think as below as your Main class, basically the Stage
// Note: The code below is for OSX Playground, not iOS

// this imports higher level APIs like Starling

import SpriteKit
import XCPlayground


// our main logic inside this class

class GameScene: SKScene {
// properties initialization
// note that the spriteNode property below is not initialized

// we initialize it through the init initializer below

var spriteNode: SKSpriteNode
var i = 0.0
var locX = 0
var locY = 250
    // this is our initializer, called once when the scene is created
    // we do our initialization/setup here
    init(size: CGSize){
        // let's grab an image, like [Embed] in AS3, results in image data like BitmapData
        // let is to declare a constant, var a variable
        // note that we don't type things, you actually can to resolve ambiguity sometimes
        // but it is inferred by default and does not cause performance issues to not statically type
        let sprite = NSImage(contentsOfFile:"/Users/bencroughs/Pictures/Donatello_LEGO.png")
         // let's create a bitmap, like Bitmap in AS3
        let myTexture = SKTexture(image: sprite)
        // let's wrap it inside a node
        spriteNode = SKSpriteNode(texture: myTexture)
        // we position it, we could scale it, etc.
        spriteNode.position = CGPoint (x: locX, y: locY)
        // we complete the initialization by initializating the superclass
         super.init(size: size)
    }
     // this gets triggered automtically when the scene is presented by the view
     // similar to Event.ADDED_TO_STAGE
    override func didMoveToView(view: SKView) {
        // let's add it to the display list
       self.addChild(spriteNode)
     }
    // we override update, which is like an Event.ENTER_FRAME or advanceTime in Starling
    override func update(currentTime: CFTimeInterval) {
        i += 0.1
               locX = locX + 5

        if (locX > 280)
        {

            locX = 280
        }

        spriteNode.position = CGPoint(x: locX, y: locY)
    }

}
// we create our scene (from our GameScene above), like a main canvas
let scene = GameScene(size: CGSize(width: 500, height: 500))

// we need a view
let view = SKView(frame: NSRect(x: 0, y: 0, width: 500, height: 500))
// we link both

view.presentScene(scene)





// display it, XCPShowView is a global function that paints the final scene

XCPShowView("result", view)

答案 4 :(得分:1)

这是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 ,请确保通过在Playground工具栏中或通过菜单选择带有两个互锁圆圈的按钮来显示助理编辑器
查看&gt;助理编辑&gt;显示助理  编辑器