因此,我想使用加速图形引擎,因此用CALayer/CAShaperLayer绘制一些形状。我想要一个100x100的视图和一个50x50的圆角矩形。请注意,这是在MacOS环境中,而不是在iOS环境中。我有以下代码不起作用:
struct KeyboardView2: NSViewRepresentable {
func makeNSView(context: Context) -> NSView {
var view = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
var shapeLayer = CAShapeLayer()
shapeLayer.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
let path = NSBezierPath(roundedRect: NSRect(x: 0, y: 0, width: 50, height: 50), xRadius: 10, yRadius: 10)
NSColor.red.setFill()
NSColor.blue.setStroke()
path.stroke()
view.layer = CALayer()
view.layer!.frame = NSRect(x: 0, y: 0, width: 100, height: 100)
view.layer!.addSublayer(shapeLayer)
return view
}
func updateNSView(_ nsView: NSViewType, context: Context) {
}
}
艾米的主意?感谢
您的绘图代码位于makeNSView
方法中。您希望将其放入draw
方法中。