在 SwiftUI for macOS 应用程序中获取点击手势的鼠标/指针位置

Posted

技术标签:

【中文标题】在 SwiftUI for macOS 应用程序中获取点击手势的鼠标/指针位置【英文标题】:Get mouse/pointer position for tap gestures in SwiftUI for macOS apps 【发布时间】:2021-03-03 19:52:55 【问题描述】:

由于 SwiftUI 中的DragGesture,目前我们可以在拖动手势期间轻松跟踪视图内的指针位置,因为该手势公开了起始位置和相对于给定坐标空间的平移:

struct Example: View 
    @GestureState private var startPosition: CGPoint = .zero
    @GestureState private var translation: CGSize = .zero
    
    var body: some View 
        Rectangle()
            .fill(Color.black)
            .frame(width: 600, height: 400)
            .coordinateSpace(name: "rectangle")
            .overlay(coordinates)
            .gesture(dragGesture)
    
    
    var dragGesture: some Gesture 
        DragGesture(minimumDistance: 5, coordinateSpace: .named("rectangle"))
            .updating($startPosition)  (value, state, _) in
                state = value.startLocation
            
            .updating($translation)  (value, state, _) in
                state = value.translation
            
    
    
    var coordinates: some View 
        Text("\(startPosition.x) \(startPosition.y) \(translation.width) \(translation.height)")
            .foregroundColor(.white)
            .font(Font.body.monospacedDigit())
    

相反,TapGesture 没有提供任何关于点击位置的信息,它只在点击发生时发出信号:

var tapGesture: some Gesture 
    TapGesture()
        .onEnded  _ in
            print("A tap occurred")
        


有没有一种简单的方法可以在点击手势发生时获取指针位置?

我在网上找到了一些解决方案,但它们通常需要大量样板文件或不方便。

一些 UIKit 的包装器认为可以解决问题,知道吗?

【问题讨论】:

这可能会有用***.com/questions/56513942/… 您也可以通过拖动手势获取点击位置 【参考方案1】:

编辑

我通过尝试同时使用手势找到了更好的解决方案。该解决方案与 SwiftUI 手势完美集成,并且与其他现有手势类似:

import SwiftUI

struct ClickGesture: Gesture 
    let count: Int
    let coordinateSpace: CoordinateSpace
    
    typealias Value = SimultaneousGesture<TapGesture, DragGesture>.Value
    
    init(count: Int = 1, coordinateSpace: CoordinateSpace = .local) 
        precondition(count > 0, "Count must be greater than or equal to 1.")
        self.count = count
        self.coordinateSpace = coordinateSpace
    
    
    var body: SimultaneousGesture<TapGesture, DragGesture> 
        SimultaneousGesture(
            TapGesture(count: count),
            DragGesture(minimumDistance: 0, coordinateSpace: coordinateSpace)
        )
    
    
    func onEnded(perform action: @escaping (CGPoint) -> Void) -> _EndedGesture<ClickGesture> 
        ClickGesture(count: count, coordinateSpace: coordinateSpace)
            .onEnded  (value: Value) -> Void in
                guard value.first != nil else  return 
                guard let location = value.second?.startLocation else  return 
                guard let endLocation = value.second?.location else  return 
                guard ((location.x-1)...(location.x+1)).contains(endLocation.x),
                      ((location.y-1)...(location.y+1)).contains(endLocation.y) else 
                    return
                
                
                action(location)
            
    


extension View 
    func onClickGesture(
        count: Int,
        coordinateSpace: CoordinateSpace = .local,
        perform action: @escaping (CGPoint) -> Void
    ) -> some View 
        gesture(ClickGesture(count: count, coordinateSpace: coordinateSpace)
            .onEnded(perform: action)
        )
    
    
    func onClickGesture(
        count: Int,
        perform action: @escaping (CGPoint) -> Void
    ) -> some View 
        onClickGesture(count: count, coordinateSpace: .local, perform: action)
    
    
    func onClickGesture(
        perform action: @escaping (CGPoint) -> Void
    ) -> some View 
        onClickGesture(count: 1, coordinateSpace: .local, perform: action)
    


以前的解决方法

基于评论中链接的this SO question,我调整了适用于 macOS 的 UIKit 解决方案。

除了更改类型之外,我还添加了一个计算来计算 macOS 坐标系样式中的位置。仍然有一个丑陋的力量展开,但我不知道足够的 AppKit 来安全地删除它。

如果您想在一个视图上同时使用多个手势,则应在其他手势修饰符之前使用此修饰符。

目前CoordinateSpace 参数仅适用于.local.global,但不适用于.named(Hashable)

如果您对其中一些问题有解决方案,我会更新答案。

代码如下:

public extension View 
    func onTapWithLocation(count: Int = 1, coordinateSpace: CoordinateSpace = .local, _ tapHandler: @escaping (CGPoint) -> Void) -> some View 
        modifier(TapLocationViewModifier(tapHandler: tapHandler, coordinateSpace: coordinateSpace, count: count))
    


fileprivate struct TapLocationViewModifier: ViewModifier 
    let tapHandler: (CGPoint) -> Void
    let coordinateSpace: CoordinateSpace
    let count: Int
    
    func body(content: Content) -> some View 
        content.overlay(
            TapLocationBackground(tapHandler: tapHandler, coordinateSpace: coordinateSpace, numberOfClicks: count)
        )
    


fileprivate struct TapLocationBackground: NSViewRepresentable 
    let tapHandler: (CGPoint) -> Void
    let coordinateSpace: CoordinateSpace
    let numberOfClicks: Int
    
    func makeNSView(context: NSViewRepresentableContext<TapLocationBackground>) -> NSView 
        let v = NSView(frame: .zero)
        let gesture = NSClickGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.tapped))
        gesture.numberOfClicksRequired = numberOfClicks
        v.addGestureRecognizer(gesture)
        return v
    
    
    final class Coordinator: NSObject 
        let tapHandler: (CGPoint) -> Void
        let coordinateSpace: CoordinateSpace
        
        init(handler: @escaping ((CGPoint) -> Void), coordinateSpace: CoordinateSpace) 
            self.tapHandler = handler
            self.coordinateSpace = coordinateSpace
        
        
        @objc func tapped(gesture: NSClickGestureRecognizer) 
            let height = gesture.view!.bounds.height
            var point = coordinateSpace == .local
                ? gesture.location(in: gesture.view)
                : gesture.location(in: nil)
            point.y = height - point.y
            tapHandler(point)
        
    
    
    func makeCoordinator() -> TapLocationBackground.Coordinator 
        Coordinator(handler: tapHandler, coordinateSpace: coordinateSpace)
    
    
    func updateNSView(_: NSView, context _: NSViewRepresentableContext<TapLocationBackground>)  

【讨论】:

以上是关于在 SwiftUI for macOS 应用程序中获取点击手势的鼠标/指针位置的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI for MacOS 窗口,带圆角,不带标题栏

(SwiftUI, MacOS 11.0) 如何在 SecureTextFeild 中检测焦点?

为啥 macOS 中的 SwiftUI 多行换行文本在 Preview 中有效,但在实际应用中无效?

在 MacOS 上的 SwiftUI 中查找点击位置

让 SwiftUI 应用出现在 macOS Dock 中

如何在 SwiftUI MacOS App 中使用 NSTouchBar?