Xcode 8 Playground 不显示 NSView
Xcode 8 Playground doesn't display NSView
Xcode 不会在 playground 中显示 NSView
。但是它显示 UIView
没有任何问题。这是一个错误吗?
代码:
let view = NSView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.white.cgColor
PlaygroundPage.current.liveView = view
Xcode 游乐场也很慢。有什么办法可以加快游乐场的速度吗?
UIView
和 NSView
两者的工作方式不同。您发布的代码对 UIView
足够,但对 NSView
.
不够
根据 NSView
的 Apple 文档:
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.
和
draw(_:)
draws the NSView object. (All subclasses must implement this
method, but it’s rarely invoked explicitly.)
所以,你必须继承NSView
并实现draw(_:)
代码如下:
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
输出:
在 Playground 中使用 iOS 比使用 macOS 更好,因为它很简单,而且您可以找到大量教程或答案。
Xcode 不会在 playground 中显示 NSView
。但是它显示 UIView
没有任何问题。这是一个错误吗?
代码:
let view = NSView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.layer?.backgroundColor = NSColor.white.cgColor
PlaygroundPage.current.liveView = view
Xcode 游乐场也很慢。有什么办法可以加快游乐场的速度吗?
UIView
和 NSView
两者的工作方式不同。您发布的代码对 UIView
足够,但对 NSView
.
根据 NSView
的 Apple 文档:
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.
和
draw(_:)
draws the NSView object. (All subclasses must implement this method, but it’s rarely invoked explicitly.)
所以,你必须继承NSView
并实现draw(_:)
代码如下:
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
输出:
在 Playground 中使用 iOS 比使用 macOS 更好,因为它很简单,而且您可以找到大量教程或答案。