基本上我想在 NSView 的 Swift 操场上测试图表绘制。
问问题
16770 次
5 回答
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
}
于 2014-06-04T18:51:58.050 回答
4
在您的 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)
于 2014-06-05T21:32:46.707 回答
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
要显示liveView,请确保通过在 Playground 工具栏中选择带有两个互锁圆圈的按钮或通过菜单:View > Assistant Editor > Show Assistant Editor来显示Assistant Editor 。
于 2018-04-03T05:57:21.427 回答
2
甚至更神奇:
var y: CGFloat = 0 {
willSet(y) {
}
didSet {
self.display()
}
}
于 2014-06-04T19:49:15.003 回答
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)
于 2014-07-06T10:51:32.250 回答