简体   繁体   English

如何在SKScene中设置自定义光标? (XCode,Swift4,Spritekit)

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

I want to use a custom cursor for my project. 我想为我的项目使用自定义光标。 Now, using one of the system cursors in XCode is no biggie, i simply call: 现在,在XCode中使用系统游标之一已经不算什么了,我只需调用:

NSCursor.crosshair() .set() 

(just an example) (只是一个例子)

This works fine but then I try to set my custom cursor the following way: (inside didMove: to View or inside sceneDidLoad(), it doesn't seem to matter which.) 这可以正常工作,但是然后我尝试通过以下方式设置自定义光标:(在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)

When I try to build I get an error in line 3 saying "unexpectedly found nil". 当我尝试构建时,第3行出现错误,提示“意外找到nil”。

So then I changed the code to this: 因此,我将代码更改为此:

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)

Now the project builds, but nothing happens. 现在该项目已构建,但是什么也没有发生。 The cursor doesn't change. 光标不变。

I also tried these lines here: 我也在这里尝试了以下几行:

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

Again, the project builds but the cursor doesn't change. 同样,项目已构建,但光标没有改变。 What am I doing wrong here? 我在这里做错了什么?

I tried to follow all the guidelines i could find but the Apple Documentation on cursor events is not much help since its heavily outdated and mouse related topics are generally very rare since most stuff is about ios. 我试图遵循我能找到的所有指南,但是有关光标事件的Apple文档没有太大帮助,因为它的过时和与鼠标相关的主题通常很少见,因为大多数内容都与ios有关。 Thanks in advance for your help. 在此先感谢您的帮助。

From the NSImage(byReferencingFile:) docs, NSImage(byReferencingFile:)文档中,

This method initializes the image object lazily. 此方法延迟初始化图像对象。 It does not actually open the specified file or create any image representations from its data until an app attempts to draw the image or request information about it. 在应用尝试绘制图像或请求有关该图像的信息之前,它实际上不会打开指定文件或根据其数据创建任何图像表示形式。

As a result, the image is not loaded from the file when the addCursorRect() method is called. 结果,当addCursorRect()方法时,不会从文件中加载图像。 In fact, the image size is (height:0, width:0) as shown in the following. 实际上,图像大小为(height:0,width:0),如下所示。

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

// (0.0, 0.0)

I suggest you use NSImage(named:) to create the image from a file: 我建议您使用NSImage(named:)从文件创建图像:

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

// (32.0, 32.0)

Also, addCursorRect(image:,hotSpot:) should only be called from the resetCursorRects() method. 另外,只能从resetCursorRects()方法中调用addCursorRect(image:,hotSpot:) From the docs, 从文档中

This method is intended to be invoked only by the resetCursorRects() method. 此方法只能由resetCursorRects()方法调用。 If invoked in any other way, the resulting cursor rectangle will be discarded the next time the view's cursor rectangles are rebuilt. 如果以任何其他方式调用,则在下次重建视图的光标矩形时,将丢弃结果光标矩形。

addCursorRect() is an instance method of NSView and, for SpriteKit, the view is an SKView which inherits from NSView . addCursorRect()NSView的实例方法,对于SpriteKit,该视图是SKView ,它继承自NSView Accordingly, you could subclass SKView, override the subclass's addCursorRect() method, and change the view's class (to our SKView subclass) in the Storyboard. 因此,您可以子类化SKView,重写子类的addCursorRect()方法,并在情节提要中更改视图的类(更改为我们的SKView子类)。

Alternatively, you can accomplish this by extending SKView and overriding the resetCursorRects() method. 另外,您可以通过扩展SKView并重写resetCursorRects()方法来完成此操作。 For example, 例如,

For Xcode 9.3 对于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)
        }
    }
}

For Xcode 9.2 对于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)
        }
    }
}

For older Xcode versions: 对于较旧的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 with cursor image highlighted. 突出显示光标图像的Project Navigator。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM