Can You Attach a Uigesturerecognizer to Multiple Views

Can you attach a UIGestureRecognizer to multiple views?

A UIGestureRecognizer is to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizer has a single view property gives it away:

view

The view the gesture recognizer is attached to. (read-only)

@property(nonatomic, readonly) UIView *view

Discussion You attach (or add) a gesture recognizer to a UIView object
using the addGestureRecognizer:
method.

one tap gesture on multiple UIImageView

A UIGestureRecognizer must be used with a single view only. You are using same object for both views. Try this.

override func viewDidLoad() {
// Do any additional setup after loading the view.

super.viewDidLoad()

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))

cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)


let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))

plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer2)
}

Handle UIGestureRecognizer in multiple views

You can achieve this from UIGestureRecognizerDelegate. Assign delegate to your gesture and use the extension below. The Delegate below allows gesture to receive touch only when touched outside from the collectionView.

extension SharePathViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
let point = touch.location(in: view)
return !collectionView.frame.contains(point)
}
}

If you want your gesture function to be called while the tap in cell to then simply call the function while the cell is selected.

UIGestureRecognizer in storyboard can use multiple views?

I have never tried what you describe with the Storyboard, so I cannot answer directly, but I can suggest a way to verify whether multiple gesture recognizers are created: in your gesture action method, add an NSLog statement to log the address of the recognizer (this is the first argument that is passed to the action method). In this way, you will verify whether the same recognizer is reused.

Also, log the recognizer.view property to see what is going on under the hood. If you get the same result for all the cases, then it can be optimistically inferred that the UISwipeRecognizer is not using internally its view property for anything and that that information is stored only for your action callback to be able to access it.

What would be a mystery is that you got only one gesture recognizer and different values for the view property. This would imply that the property value is changed on the fly, but I would not be able to explain how.

iOS adding tapGesture to multiple Views

I had the same problem where it only added to the last view. There might be a better solution, but I just created a tag gesture for each view and linked them to the same selector method (oneTap: in your case). In order to distinguish which view activated the method, you can just tag your views, feedsView.tag = 0; peopleView.tag = 1; and so on. Then when the method is called:

- (void)oneTap:(UIGestureRecognizer *)gesture {
int myViewTag = gesture.view.tag; // now you know which view called
// use myViewTag to specify individual actions;
}

Can we add single gesture on multiple views and make it working?

I don't think it's possible.

Please have a look at https://stackoverflow.com/a/5567684/470964.

Also https://stackoverflow.com/a/7883902/470964:
Maybe it's also a solution for your problem.
I think the answer is that the GestureRecognizer has only one view property, that will be set.

Adding Multiple UITapGestureRecognizers to single view (Cocos2d)

You can add multiple gesture recognizers to the same view. What you can't (easily) do is add multiple instances of the same gesture recognizer type (pan, swipe, double-tap, etc) to the same view.

Why?

Because as soon as the first gesture recognizer recognizes the gesture (double tap in this case) it cancels all touch events. Therefore the remaining gesture recognizers will never finish recognition, and will never fire their events.

You do not need more than one gesture recognizer of the same type. In your case, once you've received the double-tap event, it's up to you to signal the right object that it was double-tapped. Use the recognizer's position and other attributes to find, for example, the sprite that was double-tapped and then have it do whatever it needs to do.

For that reason it's good design to let the gestures be recognized by a higher-level node in your scene hierarchy (ie the UI layer) which then passes on the events to the appropriate nodes, or simply ignores it.

use tap gesture reconigzier on multiple image views

I tried your code:

add below two lines to enable the user interaction as mentioned by @matt:

image1.isUserInteractionEnabled = true
image2.isUserInteractionEnabled = true

and create two separate objects for gesture:

let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(gestureRecognizer1)

let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image2.addGestureRecognizer(gestureRecognizer2)

Output:
Sample Image

Happy coding...

Two UIGestureRecognizer on one view?

You need to make your Game Scene view the delegate of your gesture recognizer. You will also need implement its method shouldRecognizeSimultaneouslyWith as mentioned by xmasRights:

So in your Game scene declaration just add UIGestureRecognizerDelegate:

class GameScene: SKScene, SKPhysicsContactDelegate, UIGestureRecognizerDelegate {

And in your didMove(to view: SKView) method make set its delegate:

let longPress = UILongPressGestureRecognizer()
longPress.delegate = self
longPress.minimumPressDuration = 0
longPress.addTarget(self, action: #selector(longPressGesture))
view.addGestureRecognizer(longPress)

let swipeUp = UISwipeGestureRecognizer()
swipeUp.delegate = self
swipeUp.direction = .up
swipeUp.addTarget(self, action: #selector(swipeUpGesture))
view.addGestureRecognizer(swipeUp)

Also In Swift 4 you will need to add @objc to your methods

@objc func longPressGesture(_ longPress: UILongPressGestureRecognizer) {
print("longPressGesture")
}

@objc func swipeUpGesture(_ swipeUp: UISwipeGestureRecognizer) {
print("swipeUpGesture")
}

Don't forget also to add the method shouldRecognizeSimultaneouslyWith as already mentioned by xmasRights:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
print("shouldRecognizeSimultaneouslyWith")
return true
}

Handling Multiple GestureRecognizers

The UIGestureRecognizerDelegate has a special function managing simultaneous recognition of several gestures on the same object, that will do the trick.

1) Set your UIViewController to conform UIGestureRecognizerDelegate

2) Implement the following function:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

if (gestureRecognizer == mainScene.panRecognizer || gestureRecognizer == mainScene.pinchRecognizer) && otherGestureRecognizer == mainScene.tapRecognizer {
return true
}
return false
}

In this particular example we allow the tap gesture to get triggered simultaneously with panning and pinching.

3) Then just assign the delegates to the pan and pinch gesture recognizers:

override func viewDidLoad() {
// your code...

// Set gesture recognizers delegates
mainScene.panRecognizer.delegate = self
mainScene.pinchRecognizer.delegate = self
}


Related Topics



Leave a reply



Submit