简体   繁体   English

如何在swift / SwiftUI中为滑动手势设置菜单栏图标的操作

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

I'm trying to get a small test app I'm working on to pop up a mini window with a swiftUI view when the menubar icon is swipe down on.我正在尝试获取一个我正在开发的小型测试应用程序,以在向下滑动菜单栏图标时弹出一个带有 swiftUI 视图的迷你 window。 Rather than clicking on the button for the window to pop up, a two finger swipe down as if the user pulled down on the icon for the window to show up.与其单击 window 弹出的按钮,不如用两根手指向下滑动,就像用户拉下 window 的图标一样。 I'm still a bit new to swift but I have the following so far in the AppDelegate.我对 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 
...
}

The pop-up window is simply just a placeholder Text view with an NBA player's game stats.弹出窗口 window 只是一个带有 NBA 球员比赛统计数据的占位符文本视图。 I've commented out the sendAction with ' leftMouseDown ' in the code above because that works perfectly fine, but when I change it to ' swipe ' as a sendAction , it does nothing.我在上面的代码中用' leftMouseDown '注释掉了sendAction ,因为它工作得很好,但是当我把它改成' swipe '作为sendAction时,它什么也不做。 I've also tried ' beginGesture ', ' endGesture ', and ' gesture ' but all didn't work, the closest success was ' leftMouseDragged ' but not really what I'm going for.我也尝试过“ beginGesture ”、“ endGesture ”和“ gesture ”,但都没有奏效,最接近的成功是“ leftMouseDragged ”,但并不是我真正想要的。

I'm using SwiftUI and not storyboard if maybe that's the issue我正在使用 SwiftUI 而不是 storyboard 如果这可能是问题

There is limitation of this method, as documented only specific masks are checked by this method, swipe is not.这种方法有局限性,因为记录了这种方法只检查特定的掩码, 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