簡體   English   中英

如何在swift / SwiftUI中為滑動手勢設置菜單欄圖標的操作

[英]How to set an action to menubar icon on swipe gesture in swift/SwiftUI

我正在嘗試獲取一個我正在開發的小型測試應用程序,以在向下滑動菜單欄圖標時彈出一個帶有 swiftUI 視圖的迷你 window。 與其單擊 window 彈出的按鈕,不如用兩根手指向下滑動,就像用戶拉下 window 的圖標一樣。 我對 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 
...
}

彈出窗口 window 只是一個帶有 NBA 球員比賽統計數據的占位符文本視圖。 我在上面的代碼中用' leftMouseDown '注釋掉了sendAction ,因為它工作得很好,但是當我把它改成' swipe '作為sendAction時,它什么也不做。 我也嘗試過“ beginGesture ”、“ endGesture ”和“ gesture ”,但都沒有奏效,最接近的成功是“ leftMouseDragged ”,但並不是我真正想要的。

我正在使用 SwiftUI 而不是 storyboard 如果這可能是問題

這種方法有局限性,因為記錄了這種方法只檢查特定的掩碼, 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM