【问题标题】:How to set an action to menubar icon on swipe gesture in swift/SwiftUI如何在swift / SwiftUI中为滑动手势设置菜单栏图标的操作
【发布时间】:2020-07-13 01:05:44
【问题描述】:

我正在尝试获取一个我正在开发的小型测试应用程序,以在向下滑动菜单栏图标时弹出一个带有 swiftUI 视图的迷你窗口。与其单击弹出窗口的按钮,不如用两根手指向下滑动,就像用户拉下图标以显示窗口一样。 我对 swift 还有些陌生,但到目前为止,我在 AppDelegate 中有以下内容。

if let button = statusBar.button{
      let iconImage = NSImage(named: NSImage.Name(("iconPic")))
      button.image = iconImage
      button.action = #selector(self.handleOpenPlayerDescriptionWindow(_:))
      button.sendAction(on: .swipe)
//      button.sendAction(on: .leftMouseDown)
    }

 @objc func handleOpenPlayerDescriptionWindow(_ sender: NSMenuItem){
//code to show swiftUI Window 
...
}

弹出窗口只是一个带有 NBA 球员比赛统计数据的占位符文本视图。我在上面的代码中用 'leftMouseDown' 注释掉了 sendAction,因为它工作得很好,但是当我将它更改为 'swipe ' 作为 sendAction,它什么也不做。我也尝试过 'beginGesture'、'endGesture' 和 'gesture' 但都没有成功,最接近的成功是 ' leftMouseDragged' 但并不是我真正想要的。

如果这可能是问题的话,我使用的是 SwiftUI 而不是故事板

【问题讨论】:

    标签: swift macos swiftui swift5 swipe-gesture


    【解决方案1】:

    此方法存在局限性,据记载,此方法仅检查特定掩码,swipe 不检查。

    > Declaration
    > 
    > func sendAction(on mask: NSEvent.EventTypeMask) -> Int Discussion
    > 
    > You use this method during mouse tracking when the mouse button
    > changes state, the mouse moves, or if the cell is marked to send its
    > action continuously while tracking. Because of this, the only bits
    > checked in mask are NSLeftMouseDownMask, NSLeftMouseUpMask,
    > NSLeftMouseDraggedMask, and NSPeriodicMask, which are declared in the
    > NSEvent class reference.
    

    【讨论】:

    • 哦,真不幸。有一个名为 Unclutter 的 mac 应用程序具有类似的功能,因为它允许在菜单栏上的任意位置向下滑动以下拉应用程序的视图。至少有可能发生这样的事情吗?而不是专门在图标上,整个菜单栏被向下滑动可以显示一个应用程序视图或只是一个行动号召。
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多