How to use Swift playground to display NSView with some drawing?

青春壹個敷衍的年華 提交于 2020-01-12 04:44:09

问题


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


回答1:


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:


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 ...

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:


Even a bit more magic:

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



回答4:


This is the answer for Swift 4.1 on Xcode 9.3:

//: 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.




回答5:


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)

// 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)


来源:https://stackoverflow.com/questions/24045339/how-to-use-swift-playground-to-display-nsview-with-some-drawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!