1
我試圖快速學習。到目前爲止,我的代碼運行,但它不顯示的CALayer創建:CALayer無法看到
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
var mainview: NSView!
var redLayer: CATextLayer!
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.mainview = NSView(frame: NSRect(x:0,y:0,width:300,height:300))
self.window = NSWindow(contentRect: NSMakeRect(0, 0, 300, 300),
styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask,
backing: .Buffered, `defer`: false)
self.window.contentView!.addSubview(self.mainview)
self.window.makeKeyAndOrderFront(self.window)
self.window.makeMainWindow()
self.redLayer = CATextLayer()
self.redLayer.string = "test"
self.redLayer.frame = self.mainview.bounds
self.redLayer.bounds = CGRect(x:50,y:50,width:300,height:300)
self.redLayer.position = CGPoint(x: 50, y: 50)
self.redLayer.backgroundColor = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0)
self.redLayer.foregroundColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0)
self.redLayer.hidden = false
self.mainview.layer = self.redLayer
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}}
代碼給出了一個灰色的空窗口。但我希望它顯示一段文字說「測試」的彩色窗口,但我看不到這些東西。
我嘗試了多種方式,無法解決此問題。
確實,這解決了問題! –