How to Use Swift Playground to Display Nsview with Some Drawing

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

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
}

Swift: How to draw UIs in Playground?

Charts appear in the Assistant editor when you have something like a for loop, or for anything where you press the quick look or circle/plus buttons when mousing over the results area. See Viewing Results of an Executed Statement in Playground Help.

Xcode 8 Playground doesn't display NSView

UIView and NSView both are different in their workings. The code you posted is enough for UIView but not for NSView.

According to Apple Documentation for NSView:

An NSView object provides the infrastructure for drawing, printing,
and handling events in an app. You typically don’t use NSView objects
directly. Instead, you use objects whose classes descend from NSView
or you subclass NSView yourself and override its methods to implement
the behavior you need.

and

draw(_:) draws the NSView object. (All subclasses must implement this
method, but it’s rarely invoked explicitly.)

So, you must inherit NSView and implement draw(_:)

Here is the code:

import Cocoa
import PlaygroundSupport

class view: NSView
{
override init(frame: NSRect)
{
super.init(frame: frame)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func draw(_ dirtyRect: NSRect)
{
NSColor.blue.setFill()
NSRectFill(self.bounds)
}
}

var v = view(frame: NSRect(x: 0, y: 0, width: 200, height: 200))

PlaygroundPage.current.liveView = v

Output:

Sample Image


It is better to use iOS than macOS in Playground because it is easy and also you can find a ton of tutorials or answers.

Create textfield programmatically inside a graph subclass of NSView with swift in osx

You need to add the label as a subview of the view that will contain it:

let label = showLabel(...)
self.addSubview(label)

Try that and see if it works. BTW, you shouldn't put this code in drawRect. It will end up adding a new label every time the view is redrawn.

Mouse events (NSTrackingArea) in Playground not working

According to this 2014 WWDC session:

There are few more limitations with Playgrounds.
Playgrounds cannot be used for things which require user interaction.
So we have great support for showing live views but you can only see them, you can't touch them.

You can find the original video here

How do I draw a line in iOS swift in the viewDidLoad() function?

From the comments it sounds like you just want to add a line to your user interface programmatically. To do that, just use a UIView to create the line. Set its backgroundColor to the color you want. The height you specify for the frame will be the thickness of the line.

// Add a green line with thickness 1, width 200 at location (50, 100)
let line = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 1))
line.backgroundColor = UIColor.greenColor()
self.view.addSubview(line)


Related Topics



Leave a reply



Submit