[英]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.