我想为我的项目使用自定义光标。现在,在 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 的。在此先感谢您的帮助。