web-dev-qa-db-ja.com

Swiftプレイグラウンドを使用してNSViewを描画で表示するにはどうすればよいですか?

基本的に、NSViewのSwift playgroundでチャートの描画をテストしたいと思います。

11
Alfa07

これが私が今使っているものです:

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
}
8
Alfa07

xcode Swiftプレイグラウンド環境でこのコードを試してください。drawは複数回実行されるため、すべてのオブジェクトの初期化を必ずinitに入れてください:-)、これは単なるサンプルであり、今後さらに増える予定です。 ..

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)
4
Ben Croughs

これは、Xcode9.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を表示するには、Assistant Editorを選択して表示してください。 Playgroundツールバーまたはメニューを介した2つの連動する円のあるボタン:
[表示]> [アシスタントエディター]> [アシスタントエディターを表示]

2
MartinW

さらにもう少し魔法:

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

このコードを確認してください。カメの中でスライドします(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)
1
Ben Croughs