簡體   English   中英

如何在SKScene中設置自定義光標? (XCode,Swift4,Spritekit)

[英]How to set a custom cursor in an SKScene? (XCode, Swift4, Spritekit)

我想為我的項目使用自定義光標。 現在,在XCode中使用系統游標之一已經不算什么了,我只需調用:

NSCursor.crosshair() .set() 

(只是一個例子)

這可以正常工作,但是然后我嘗試通過以下方式設置自定義光標:(在didMove內部:要查看或在sceneDidLoad()內部,似乎無關緊要。)

let image = NSImage(named: "hand1.png")
let spot = NSPoint(x: 4, y: 8)
let customCursor = NSCursor(image: image!, hotSpot: spot)
self.view!.addCursorRect(self.frame, cursor:customCursor)

當我嘗試構建時,第3行出現錯誤,提示“意外找到nil”。

因此,我將代碼更改為此:

let image = NSImage(byReferencingFile: "hand1.png")
let spot = NSPoint(x: 4, y: 8)
let customCursor = NSCursor(image: image!, hotSpot: spot)
self.view!.addCursorRect(self.frame, cursor: customCursor)

現在該項目已構建,但是什么也沒有發生。 光標不變。

我也在這里嘗試了以下幾行:

NSCursor.init(image: image!, hotSpot: spot)
NSCursor.set(customCursor)()

同樣,項目已構建,但光標沒有改變。 我在這里做錯了什么?

我試圖遵循我能找到的所有指南,但是有關光標事件的Apple文檔沒有太大幫助,因為它的過時和與鼠標相關的主題通常很少見,因為大多數內容都與ios有關。 在此先感謝您的幫助。

NSImage(byReferencingFile:)文檔中,

此方法延遲初始化圖像對象。 在應用嘗試繪制圖像或請求有關該圖像的信息之前,它實際上不會打開指定文件或根據其數據創建任何圖像表示形式。

結果,當addCursorRect()方法時,不會從文件中加載圖像。 實際上,圖像大小為(height:0,width:0),如下所示。

if let image = NSImage(byReferencingFile:"cursorImage.png") {
    print("\(image.size)")
}

// (0.0, 0.0)

我建議您使用NSImage(named:)從文件創建圖像:

if let image = NSImage(named:NSImage.Name("cursorImage.png")) {
    print("\(image.size)")
}

// (32.0, 32.0)

另外,只能從resetCursorRects()方法中調用addCursorRect(image:,hotSpot:) 從文檔中

此方法只能由resetCursorRects()方法調用。 如果以任何其他方式調用,則在下次重建視圖的光標矩形時,將丟棄結果光標矩形。

addCursorRect()NSView的實例方法,對於SpriteKit,該視圖是SKView ,它繼承自NSView 因此,您可以子類化SKView,重寫子類的addCursorRect()方法,並在情節提要中更改視圖的類(更改為我們的SKView子類)。

另外,您可以通過擴展SKView並重寫resetCursorRects()方法來完成此操作。 例如,

對於Xcode 9.3

extension SKView {
    override open func resetCursorRects() {
        if let image = NSImage(named:NSImage.Name(rawValue:"cursorImage.png")) {
            let spot = NSPoint(x: 0, y: 0)
            let customCursor = NSCursor(image: image, hotSpot: spot)
            addCursorRect(visibleRect, cursor:customCursor)
        }
    }
}

對於Xcode 9.2

extension SKView {
    override open func resetCursorRects() {
        if let image = NSImage(named:NSImage.Name("cursorImage.png")) {
            let spot = NSPoint(x: 0, y: 0)
            let customCursor = NSCursor(image: image, hotSpot: spot)
            addCursorRect(visibleRect, cursor:customCursor)
        }
    }
}

對於較舊的Xcode版本:

extension SKView {
    override open func resetCursorRects() {
        if let path = Bundle.main.path(forResource: "cursorImage", ofType: "png") {
            if let image = NSImage(contentsOfFile:path) {
                let spot = NSPoint(x: 0, y: 0)
                let customCursor = NSCursor(image: image, hotSpot: spot)
                addCursorRect(visibleRect, cursor:customCursor)
            }
        }
    }
}

突出顯示光標圖像的Project Navigator。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM