为什么这个swift代码没有显示出来行和动画?

wlzqhblo  于 2023-09-30  发布在  Swift
关注(0)|答案(2)|浏览(98)

为什么MyViewController没有得到的显示在窗口中,虽然它的规定在contentViewController: MyViewController()
我不是在故事板或XIB,我不想使用笔尖文件。

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create a new window
        window = NSWindow(contentViewController: MyViewController())
        // Make the window visible
        window.makeKeyAndOrderFront(nil)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }
}

    
class MyViewController: NSViewController {
    
    override func loadView() {
        let view = NSView(frame: NSMakeRect(0, 0, 400, 400))
        self.view = view
        
        // Create a shape layer for the rectangle
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = NSColor.black.cgColor
        shapeLayer.lineWidth = 2
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPattern = [NSNumber(value: 6), NSNumber(value: 4)]
        
        // Create a path for the rectangle
        let path = CGMutablePath()
        path.addRect(NSRect(x: 50, y: 50, width: 100, height: 100))
        shapeLayer.path = path
        
        // Create an animation for the dashed line
        let animation = CABasicAnimation(keyPath: "lineDashPhase")
        animation.fromValue = 0
        animation.toValue = shapeLayer.lineDashPattern?.reduce(0) { $0 + $1.intValue }
        animation.duration = 1
        animation.repeatCount = .infinity
        shapeLayer.add(animation, forKey: "lineDashPhase")
        
        // Add the shape layer to the view hierarchy
        view.layer?.addSublayer(shapeLayer)
    }
    
}
q5iwbnjs

q5iwbnjs1#

我不是在故事板或XIB,我不想使用笔尖文件。
如果你想使用编程方法,那么你不需要基本窗口的控制器,如下面的演示所示。要在Xcode中运行下面的代码,首先创建一个Swift项目,添加一个'main.swift'文件,然后删除预先提供的AppDelegate文件。将此代码复制/粘贴到'main'文件中并运行。我在视图中添加了一个绿色背景,以便您可以看到它的边界。您需要将.wantsLayer设置为true,然后将视图作为子视图添加到window. contentView。

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
    var window:NSWindow!
    var view: NSView!
    
    func buildMenu() {
        let mainMenu = NSMenu()
        NSApp.mainMenu = mainMenu
        // **** App menu **** //
        let appMenuItem = NSMenuItem()
        mainMenu.addItem(appMenuItem)
        let appMenu = NSMenu()
        appMenuItem.submenu = appMenu
        appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
    }
    
    func buildWnd() {
        
        let _wndW : CGFloat = 400
        let _wndH : CGFloat = 400
        
        window = NSWindow(contentRect:NSMakeRect(0,0,_wndW,_wndH),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing:.buffered, defer:false)
        window.center()
        window.title = "Swift Test Window"
        window.makeKeyAndOrderFront(window)
        
        // **** CAShapeLayer with Animation **** //
        view = NSView(frame: NSMakeRect(0, 0, 400, 400))
        window.contentView!.addSubview (view)
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.green.cgColor
        
        // Create a shape layer for the rectangle
        let shapeLayer = CAShapeLayer()
        shapeLayer.strokeColor = NSColor.black.cgColor
        shapeLayer.lineWidth = 2
        shapeLayer.lineJoin = .round
        shapeLayer.lineDashPattern = [NSNumber(value: 6), NSNumber(value: 4)]
        
        // Create a path for the rectangle
        let path = CGMutablePath()
        path.addRect(NSRect(x: 50, y: 50, width: 100, height: 100))
        shapeLayer.path = path
        
        // Create an animation for the dashed line
        let animation = CABasicAnimation(keyPath: "lineDashPhase")
        animation.fromValue = 0
        animation.toValue = shapeLayer.lineDashPattern?.reduce(0) { $0 + $1.intValue }
        animation.duration = 1
        animation.repeatCount = .infinity
        shapeLayer.add(animation, forKey: "lineDashPhase")
        
        // Add the shape layer to the view hierarchy
        view.layer?.addSublayer(shapeLayer)
        
        // **** Quit btn **** //
        let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
        quitBtn.bezelStyle = .circular
        quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
        quitBtn.title = "Q"
        quitBtn.action = #selector(NSApplication.terminate)
        window.contentView!.addSubview(quitBtn)
    }
    
    func applicationDidFinishLaunching(_ notification: Notification) {
        buildMenu()
        buildWnd()
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        return true
    }
    
}
let appDelegate = AppDelegate()

// **** main.swift **** //
let app = NSApplication.shared
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()

0ve6wy6x

0ve6wy6x2#

看起来你的问题至少有一部分与Willeke链接的答案相同(来自Matt)
问题是你从没说过
view.wantsLayer = true
所以你的视图没有图层,对图层应用背景色没有效果,因为(正如我刚才所说的)你的视图没有图层。所以风景就在那里,但你看不到它;它是透明的,就像隐形人一样。
在创建视图后,尝试添加view.wantsLayer = true行。

相关问题