SwiftUI系统游标 [英] SwiftUI System Cursor

查看:48
本文介绍了SwiftUI系统游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 MacOS 上的 SwiftUI 中将光标更改为十字准线.

I'm trying to change the cursor to a crosshair in SwiftUI on MacOS.

我已将以下代码放入 AppDelegate applicationDidFinishLaunching()函数中:

I've put the following code into the AppDelegate applicationDidFinishLaunching() function:

NSCursor.crosshair.set()

当应用程序加载时,我看到光标更改为crossHair,然后直接交换回标准指针.

When the application loads, I see the cursor change to the crossHair, and then swap straight back to the standard pointer.

很高兴听到我在这里做错了什么.

It would be great to hear what I'm doing wrong here.

谢谢.

推荐答案

这不是 NSCursor 的工作方式.有一堆游标,因此任何标准(或非标准)控件都可以将自己的游标类型推到顶部,并使该游标成为当前游标.因此,您只需放置第一个光标,然后使用一些具有自己默认光标的标准视图替换它.

It is not the way how NSCursor works. There is a stack of cursors so any standard (or non-standard) control can push own type of cursor on top and make that cursor current. So you just place first cursor, but then some standard view having own default cursor replaces it.

由于SwiftUI现在不允许本地管理游标,因此解决方案可能是

As SwiftUI does not allow now to manage cursors natively, the solution might be

a)设置/在SwiftUI视图上显示/消失所需的光标,或

a) either to set/push desired cursor on SwiftUI view appear/disappear, or

b)使用标准的 NSView.addCursorRect 方法将光标rect添加到 NSHostingController 视图.

b) add cursor rect to NSHostingController view using standard NSView.addCursorRect method.

更新:来自现有项目的一些快速演示(自定义按钮解决方案的一部分)

Update: some quick demo from existing project (part of custom button solution)

struct DemoCustomCursor: View {
    var body: some View {
        Button(action: {}) {
            Text("Cross Button")
                .padding(20)
                .background(Color.blue)
        }.buttonStyle(PlainButtonStyle())
        .onHover { inside in
            if inside {
                NSCursor.crosshair.push()
            } else {
                NSCursor.pop()
            }
        }
    }
}

这篇关于SwiftUI系统游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆