为什么这段快速代码不显示线条和动画?

问题描述 投票:0回答:1

为什么

MyViewController
没有显示在窗口中,尽管
contentViewController: MyViewController()
中已说明?

我不在情节提要或 XIB 上,我不想使用 nib 文件。

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)
    }
    
}
swift cocoa nsviewcontroller
1个回答
0
投票

我不在情节提要或 XIB 上,我不想使用 nib 文件。

如果您想使用编程方法,那么您不需要基本窗口的控制器,如下面的演示所示。要在 Xcode 中运行以下命令,首先创建一个 Swift 项目,添加一个“main.swift”文件,然后删除预先提供的 AppDelegate 文件。将此代码复制/粘贴到“主”文件中并运行。我在您的视图中添加了绿色背景,以便您可以看到其边界。您需要将 .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()

© www.soinside.com 2019 - 2024. All rights reserved.