如何使用Swift游乐场显示NSView与一些绘图?

时间:2022-06-01 16:52:28

Basically I would like to test a chart drawing in a Swift playground in NSView.

基本上,我想在NSView的一个快速操场上测试一下图表。

5 个解决方案

#1


8  

Here is what I am using now:

下面是我现在使用的:

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
}

#2


4  

try this code in your xcode swift playground environment, be sure to put all object initialization in init, because draw gets executed multiple times :-), this is just a simple sample, more to come ...

在您的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)

#3


2  

Even a bit more magic:

更神奇的是:

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

#4


1  

check this code, it slides in a Turtles (mind the location of the PNG file, you probably need to adapt this to your own location /file)

检查这段代码,它会在海龟中滑动(注意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)

#5


0  

This is the answer for Swift 4.1 on Xcode 9.3:

这是关于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

To display the liveView, make sure to show the Assistant Editor by selecting the button with the two interlocking circles in the Playground toolbar or via the menu:
View > Assistant Editor > Show Assistant Editor.

要显示liveView,请确保通过在游乐场工具栏中选择带有两个连锁圆的按钮或通过菜单显示助理编辑器:查看>助理编辑器> show Assistant编辑器。

#1


8  

Here is what I am using now:

下面是我现在使用的:

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
}

#2


4  

try this code in your xcode swift playground environment, be sure to put all object initialization in init, because draw gets executed multiple times :-), this is just a simple sample, more to come ...

在您的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)

#3


2  

Even a bit more magic:

更神奇的是:

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

#4


1  

check this code, it slides in a Turtles (mind the location of the PNG file, you probably need to adapt this to your own location /file)

检查这段代码,它会在海龟中滑动(注意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)

#5


0  

This is the answer for Swift 4.1 on Xcode 9.3:

这是关于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

To display the liveView, make sure to show the Assistant Editor by selecting the button with the two interlocking circles in the Playground toolbar or via the menu:
View > Assistant Editor > Show Assistant Editor.

要显示liveView,请确保通过在游乐场工具栏中选择带有两个连锁圆的按钮或通过菜单显示助理编辑器:查看>助理编辑器> show Assistant编辑器。