Having problems implementing both pinch and 2-touch pan gesture simultaneously - ios

What I have is my view controllers view with several subviews. The hierarchy looks something like:
[view controller view]
[container view]
[view1,view2,view3,view4,view5,etc...]
[gesture view (full screen)]
I'm trying to implement pinch and 2-finger panning, but cannot get them to work simultaneously:
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinch:)];
pinch.delegate = self;
UIPanGestureRecognizer *pullDownContainerView = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(pullingDownContainerView:)];
pullDownContainerView.minimumNumberOfTouches = 2;
pullDownContainerView.maximumNumberOfTouches = 2;
pullDownContainerView.delegate = self;
[self.touchView addGestureRecognizer:pullingDownContainerView];
[self.touchView addGestureRecognizer:pinch];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Another strange thing is that shouldRecognizeSimultaneouslyWithGestureRecognizer isn't being called (I have implemented the UIGestureRecognizerDelegate protocol).
They do work individually. I've read something about using a scroll view where pan and pinch are readily available or something. So I'd need to replace the container view's UIView with a UIScrollView
Would like some guidance.
Update:
Did a quick clean and build and now it's working!

Related

iOS UITapGestureRecognizer and UIWebView

I have a viewcontroller, which contains a web view.
I'd like to add two gesturerocignizers to the viewController's view:
One UITapGestureRecognizer and one UILongPressureGestureRecognizer.
Here's the code:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(backToDashboards:)];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(backToDashboards:)];
doubleTapGesture.numberOfTapsRequired = 2;
doubleTapGesture.delegate = self;
[self.view addGestureRecognizer:longPressGesture];
[self.view addGestureRecognizer:doubleTapGesture];
My view hierarchy:
-ViewController
|
|-View (added the gesture recognizers for this view)
|-WebView
I've added the shouldRecognizeSimultaneouslyWithGestureRecognizer method with return YES
But if I long press the screen nothing happens, only if the press' location is on a place where nothing can be scrolled in the webview.
The double tap recognizer doesn't work at all.
Any idea?
Thank you in advance!
I suspect that because your webview is on top of your view, the app doesnt know if you are tapping the webview or self.view. Check out this reply How to detect of tap gesture is from uiwebview or self.view?
try with below code.Where u r adding gester in you code?
below code is working for me.
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPress.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:longPress];
// if u want to add gesture for u webview,
//[self.webview addGestureRecognizer:longPress]
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
}
//You should enable simultaneous gesture recognition
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

Conflicting UIPanGestureRecognizer (3 fingers) and UIPinchGestureRecognizer

I'm adding both a UIPanGestureRecognizer and UIPinchGestureRecognizer to the same view. This typically would not cause any issues, but the requirement for 3 fingers with my UIPanGestureRecognizer is causing problems:
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinched:)];
[self.view addGestureRecognizer:pinchGesture];
UIPanGestureRecognizer panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panning:)];
panGesture.minimumNumberOfTouches = 3;
panGesture.maximumNumberOfTouches = 3;
[self.view addGestureRecognizer:panGesture];
Occasionally, the pinch gesture will get called when the pan should have been. It works ~50% of the time, but what is a better way to implement these two gestures on the same view so the accuracy is better?
Edit: I only want one gesture to happen at a time.
Set the delegates for the gestures to self and set your ViewController to implement UIGestureRecognizerDelegate
And add this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
other post has a typo I think, but use this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return NO;
}
and add this so other gestures will fail before the gesture you want to start
requireGestureRecognizerToFail

How to detect of tap gesture is from uiwebview or self.view?

Hello everyone I have a uitapgesture on my view using the following code :
UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(TAPGestureRecognizer)];
tap.numberOfTapsRequired=1;
tap.delegate = self;
[self.view addGestureRecognizer:tap];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And I have a uiwebview as a subview in my UIview. The problem is that on the uivewview HTML it had a onclick(); which in turn is calling the tapgesture. Any help?
FirstView.image=[UIImage imageNamed:#"shape1.jpg"];
FirstView.tag=1;
FirstView.userInteractionEnabled=YES;
[FirstView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(firstimagetouch:)]];
SecondWebView.tag=2;
SecondWebView.userInteractionEnabled=YES;
[SecondWebView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(firstimagetouch:)]];
-(void)firstimagetouch:(UIGestureRecognizer *)sender
{
UIView * view=sender.view;
NSLog(#"%ld",(long)view.tag);
}
try like this first add gesture recognizer to view and then your webview according to give him tag value and get tag value. and do what you want.

How to implement swipe gesture on half view and pan gesture on other half view?

How to add a pan gesture to first half (0,0,160,480) of a view and on same view a swipe gesture on (160,0,160,480) as well?
The view is table view and on swiping left the cell content should change and on panning in right the tableview should move like facebook ios app
Take two Different UIview as SubView of mainView with Required Frame Size then add Desired Gestures :-
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeVC:)];
swipeGestureRecognizer.delegate = self;
[self.subView.view addGestureRecognizer:swipeGestureRecognizer];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panVC:)];
panGestureRecognizer.delegate = self;
[panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
[self.subView2.view addGestureRecognizer:panGestureRecognizer];
The Below method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

Gesture Recognizer

I have a UIView on some part of screen. This UIView has Pan gesture recognizer. This UIView ( mainVw) has a UITextView that completely covers the UIView. This UITextview has swipe gestures(left and write).
My problem is When I swipe left or right, The UIView starts dragging also. What I want is ,when I drag the UIView it should drag and when I swipe UITextView should swipe left write but no dragging.
Here is the code I'm using to give better understanding:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[panGesture setMinimumNumberOfTouches:1];
panGesture.delegate =(id)self;
[self.mainVw addGestureRecognizer:panGesture];
//[self.mainVw.layer setValue:[NSValue valueWithCGPoint:vwCaptionBg.center] forKey:#"originalCenter"];
UISwipeGestureRecognizer *rightRecognizer;
rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanTouch:)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
rightRecognizer.delegate=(id)self;
[self.txtVw addGestureRecognizer:rightRecognizer];
UISwipeGestureRecognizer *vwLeftRecognizer;
vwLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanTouch:)];
[vwLeftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
vwLeftRecognizer.delegate=(id)self;
[self.txtVw addGestureRecognizer:vwLeftRecognizer];
You could try using the [GESTURE requireGestureRecognizerToFail:GESTURE_TO_FAIL]; method. This would be your swipes would need to fail before the pan would be allowed to proceed.
There is also delegate methods you can use for simultaneous gestures.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
Or you could use either of these delegate methods with your own logic.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

Resources