本文介绍了如何使用 Catalyst 实现 onHover 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 swiftUI 使用 Catalyst 进行鼠标 onHover
事件?
Is it possible for a mouse onHover
event with Catalyst using swiftUI?
onHover(perform:) 仅适用于 macOS
现在.
推荐答案
这是 Catalyst 的干净解决方案,但 iPadOS/Catalyst 13.4 可能很快就会推出.在任何情况下都使用相同的:
Here's a clean solution for Catalyst, but this might be coming soon in iPadOS/Catalyst 13.4. Use the same in any case:
someView.onHover2 { doSomething(isHovering: $0) }
支持此功能的代码:
import SwiftUI
extension View {
func onHover2(perform action: @escaping (Bool) -> Void) -> some View {
return self.overlay(HoverRecognizer(action: action))
}
}
public struct HoverRecognizer: UIViewRepresentable {
var action: (Bool) -> Void
public func makeUIView(context: Context) -> UIView {
return HoverView(action)
}
public func updateUIView(_ uiView: UIView, context: Context) {
}
private class HoverView: UIView {
var action: (Bool) -> Void
init(_ action: @escaping (Bool) -> Void) {
self.action = action
super.init(frame: CGRect.zero)
self.addGestureRecognizer(UIHoverGestureRecognizer(
target: self,
action: #selector(hovering(_:))))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc
func hovering(_ recognizer: UIHoverGestureRecognizer) {
switch recognizer.state {
case .began, .changed:
action(true)
case .ended:
action(false)
default:
break
}
}
}
}
这篇关于如何使用 Catalyst 实现 onHover 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!