swift 在Mac Catalyst中获取鼠标坐标

bvhaajcl  于 5个月前  发布在  Swift
关注(0)|答案(1)|浏览(74)

我在AppKit(Getting Mouse Coordinates in Swift)中看到了类似的问题,但它在Catalyst中不起作用,因为NSViewController不可用。
如何获取Mac Catalyst中鼠标的坐标?

snvhrwxg

snvhrwxg1#

您可以将UIHoverGestureRecognizer添加到您的UIView:

let hoverGestureRecognizer = UIHoverGestureRecognizer(target: nil, action: nil)
addGestureRecognizer(hoverGestureRecognizer)

字符串
然后,您可以通过以下方式从这个手势识别器中检索鼠标指针的坐标:

public var pointerPosition: CGPoint? {
    guard (hoverGestureRecognizer.state == .began) || (hoverGestureRecognizer.state == .changed) else { return nil }
    return hoverGestureRecognizer.location(in: self)          
}

相关问题