admin管理员组文章数量:1252720
GOAL I am trying to have a reliable way to detect if the cursor enters or exit the view.
PROBLEM
It breaks when the window is zoomed or goes to full screen. After it is done resizing it will quickly set isHovering
to false
and then back to true
. So you can't detect if the window is resizing and then skip the onHover
, because it will fire after the resizing is done.
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onHover { isHovering in
print(isHovering)
}
}
}
#Preview {
ContentView()
}
GOAL I am trying to have a reliable way to detect if the cursor enters or exit the view.
PROBLEM
It breaks when the window is zoomed or goes to full screen. After it is done resizing it will quickly set isHovering
to false
and then back to true
. So you can't detect if the window is resizing and then skip the onHover
, because it will fire after the resizing is done.
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onHover { isHovering in
print(isHovering)
}
}
}
#Preview {
ContentView()
}
Share
Improve this question
asked Jan 27 at 10:05
MarkMark
18.2k23 gold badges90 silver badges128 bronze badges
1
- Have you considered using AppKit APIs to detect hover? Do they have the same problem? – Sweeper Commented Jan 27 at 13:37
1 Answer
Reset to default 0One work around seemed to be to put a debouncer between it.
@State private var onHoverPublisher = PassthroughSubject<Bool, Never>()
content
.onHover { onHoverPublisher.send($0) }
.onReceive(onHoverPublisher.debounce(for: .milliseconds(50), scheduler: DispatchQueue.main).removeDuplicates()) { isHovering in
// This is hovering is more reliable.
}
本文标签: swiftuiHow can I avoid onHover giving a false signal on macOS window resizeStack Overflow
版权声明:本文标题:swiftui - How can I avoid onHover giving a false signal on macOS window resize? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738272034a2072311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论