Weird issue with NSMenu - swift

I am showing NSMenu with below method. Mouse over events and actions on MenuItems are working fine.
if let event = NSApplication.shared.currentEvent
NSMenu.popUpContextMenu(menu, with: event, for: self.view)
}
Now, to have this menu at different position I changed this method to below and all the events like mouse over and selections are not working.
menu.popUp(positioning: item1, at: position, in: self.view)
Please suggest me solution.
Thanks in advance.

Related

SwiftUI: Sequence gestures starting with tap

I want to create the "one finger zoom gesture" from the iOS Maps App. So a tap (touch down, then touch up), then long press (only touch down), and finally a drag up/down.
I was expecting something like this:
let tapGesture = TapGesture().onEnded { _ in
print("tap")
}
let longPressGesture = LongPressGesture(minimumDuration: 0.00001)
.onChanged { _ in
print("long press update")
}
.onEnded { _ in
print("long press")
}
let dragGesture = DragGesture(minimumDistance: 1)
.onChanged { gesture in
print("drag")
}
let combined = tapGesture.sequenced(before: longPressGesture).sequenced(before: dragGesture)
SomeView()
.gesture(combined)
Only the LongPressGesture after the initial tap never gets called. I've tried a LongPressGesture instead of the initial TapGesture, but two sequential long presses are called instantly (so the "touch up" event is not regarded).
An initial DragGesture(minimumDuration: 0) with onEnded and a corresponding #State to track the initial tap might work, but I have a ScrollView beneath (for the map-like panning and zooming), so I'm guessing a DragGesture cannot be the first one.
I also tried setting a gestureEnabled flag after a first tap via onTapGesture() and then using the .gesture(combined, including: gestureEnabled ? .all : .none), with a few more edge cases handled I managed to get it working, only to find out that double taps no longer worked anywhere else on the View.
I should clarify that there's a ScrollView with text underneath the view, so long press, double tap, scrolling (horizontal and vertical) should all keep working.
Any ideas?
Currently, my best alternative is just using the long press + drag. It works with the additional tap as well (since it's not necessary in that case). Only downside is it also triggers if the user holds down for a short time before attempting a scroll.

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. 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.
I'm still a bit new to swift but I have the following so far in the 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. 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. 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.
I'm using SwiftUI and not storyboard if maybe that's the issue
There is limitation of this method, as documented only specific masks are checked by this method, swipe is not.
> 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.

Swift: Segue to another view when Map View is tapped

I would like to perform a segue to another view once a map view is tapped like when you check a location and tap on map on instagram, or like in foursquare.
I could not connect map view itself to another view directly like we do in buttons, and I don't want to perform segue when callout button is tapped.
I found a workaround by placing an invisible button into mapView, and performing segue when the button is tapped(user thinks mapView is tapped)
But I would like to know if there is a better solution for it, without a button.
You can use UITapGestureRecognizer
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapHandler(_:)))
mapView.addGestureRecognizer(gestureRecognizer)
#objc func tapHandler(_ gestureReconizer: UIGestureRecognizer) {
// let location = gestureReconizer.location(in: mapView)
// coordinatte of taplocation
// let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
// **you can navigate or perform Your Actions there**
}

Swift NSMenuItem detect shift click

I'm currently adding an NSMenuItem to my NSMenu and I'm able to successfully detect and handle the click event. However, I want to add a second option and detect if the user has clicked the menuItem while holding SHIFT
let menuItem = myMenu?.submenu?.addItemWithTitle("Click me", action: "itemClicked:", keyEquivalent: "")
Is there any way to do this? I looked at keyEquivalent but I'm not seeing ANYTHING to how to get this to work.
Thanks
Thanks to #LeoDabus for some help but I was able to solve this in the event handler for my NSMenuItem with the following code:
if let event = NSApp.currentEvent {
if event.modifierFlags.contains(.ControlKeyMask) {
// success!
}
}
happy coding

Adding long press gesture recognizer to UITextView

I'm trying to let the user move a text view around the screen as he presses down on it. The issue I'm getting is my gesture recognizer isn't getting called and instead the text is being selected when I press down on it. How can I disable this? I can't just disable user interaction because a tap should still let the user edit the text. My code is below
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
text.addGestureRecognizer(longPressRecognizer)
func longPressed(sender : UITextView) {
println("long press")
}
Try adding this tiny piece of code. Sometimes it makes magic happen ;)
text.userInteractionEnabled = true