Gesture recognizer issues - swift

I'm having trouble wrapping my brain around gesture recognizers. I want to target a subview. Here's an example that works with the main view:
let gesture = UITapGestureRecognizer(target: hex_pin_view!, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
Here's the subview target, this dose not work and is what I'm looking for:
let gesture = UITapGestureRecognizer(target: hex_pin_view!.add_emoji_img_view, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
I'm getting a crash error:
unrecognized selector sent to instance
Tho the selector is the same, I don't get it.
Anyone have a clue what I could be doing wrong?

The target of a gesture recogniser is the object that will receive messages from the gesture recogniser. or simply put, the object that holds the function that will handle the message.
Alot of the time the target: is set to self.
selector is the function/method that will handle the messages.
let gesture = UITapGestureRecognizer(target: self, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
So there, the currrent class/object should have a method called openEmojis. The gesture is added to the mapView here
mapView.addGestureRecognizer(gesture)
So when the map gets tapped, your function should be called.
If you want the gesture to trigger when your subview is tapped. instead add the gesture recogniser to the subview instead:
add_emoji_img_view.addGestureRecognizer(gesture)

Related

UISwipeGesture not registering first swipe

Here's my code in viewDidLoad:
//add a swipe gesture
let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rotateDial(_:)))
downSwipe.direction = .down
self.view.addGestureRecognizer(downSwipe)
The first time I swipe down it doesn't recognize the gesture. I read in another SO that the swipe gesture is registering itself on the first swipe. How do I fix that?

Tap Gesture isn't working. User interaction enabled.

Have following view structure:
Programmatically adding tap gesture rec to Temp lbl:
let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
tempLblTap.delegate = self
tempLblTap.numberOfTapsRequired = 1
tempLblTap.numberOfTouchesRequired = 1
tempLblTap.cancelsTouchesInView = false
self.tempLbl.isUserInteractionEnabled = true
self.tempLbl.addGestureRecognizer(tempLblTap)
but the method convertDegrees isn't triggered.
There are also 2 swipe gesture recognizers that are added to the same view:
let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(leftSwipeGestureRecognizer)
let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Maybe they are the reason?
Found solution:
View that contained my lbl had its userInteractionEnabled unchecked in storyboards.
So, when adding gesture recognizers to labels, images or simply adding buttons, always check that all parent views has its userInteractionEnabled to true.

UITapGestureRecogniser add target not working

i have a objc function:
#objc private func segmentedControlViewTapped(gestureRecognizer: UITapGestureRecognizer)
I am trying to add this as a target for a gestureRecognizer but the code keeps crashing when i tap the segment control.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SegmentedControl.segmentedControlViewTapped(gestureRecognizer:)))
crashes due to: unrecognized selector sent
The action needs to point to a function in the target you specified.
Assuming the segmentedControlViewTapped is in your view or controller, change your code like this:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(segmentedControlViewTapped(gestureRecognizer:)))

How to detect a remote click in SpriteKit and pass node information

I'm using Swift & SpriteKit.
I've got my sprite focused (using touches began and touches moved) & would like to click the remote to select it, but keep getting:
unrecognized selector sent to instance
Here's the code I'm using..
let tap = UITapGestureRecognizer(target: self, action: "processItemTouch")
tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
view.addGestureRecognizer(tap)
func processItemTouch(nod : SKNode) {
// stuff to do
}
I've tried changing the processItemTouch to processItemTouch: & processItemTouch(nod : SKNode).
Changing the selector to processItemTouch: should cause the right method to get called, but the argument that gets passed won't be an SKNode, it'll be a UITapGestureRecognizer. When gestures call their actions, the argument will be the gesture object that triggered the action.

How can I find button tag after implementing gesture recogniser in swift?

I want an action to be done once when someone swipes within a button.
My current code is as follows:
let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
recogniser.direction = .Up
button.addGestureRecognizer(recogniser)
func didTapButton2(sender: UIGestureRecognizer!) {
//Here I want to be able to recognise which button this was sent from (they are all tagged)
let button = sender. as UIButton //Gives an error
I need to use gesture recognisers as opposed to UIControlEvents as I need the event to only fire once. Using this makes the event fire loads of times - just need it to go once:
button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)
Does anyone have a solution? Thanks
A UIGestureRecognizer has a property called view which is the view that it is attached to. View is declared as an optional, so you need to unwrap it:
if let button = sender.view as? UIButton {
// use button
if button.tag == 10 {
// handle button tagged with 10
}
}