touchesBegan cancel UISwipeGestureRecognizer - ios

I have a general touchesBegan in my viewcontroller
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"--[%s:%d]",__PRETTY_FUNCTION__,__LINE__);
}
This seems to cancel the UISwipeGestureRecognizer. (not fired)
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
swipeRight.numberOfTouchesRequired=1;
swipeLeft.cancelsTouchesInView=NO;
swipeRight.cancelsTouchesInView=NO;
swipeLeft.delaysTouchesBegan = YES;
swipeRight.delaysTouchesBegan = YES;
self.viewSwipe.userInteractionEnabled=YES;
[self.viewSwipe addGestureRecognizer:swipeLeft];
[self.viewSwipe addGestureRecognizer:swipeRight];
Any idea? :)

touchesBegan:: gets called before any gesture recognizers, and will indeed override them.
If you wish to support both tap and swipe on the same view, you can use a UITapGestureRecognizer, and call requireGestureRecognizerToFail.
So, add the following to the bottom of your code sample:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer requireGestureRecognizerToFail swipeLeft];
[tapRecognizer requireGestureRecognizerToFail swipeRight];

Related

iOS: webview swipe gesture

I have 2UIWebView controls. Using these 2 webviews, I have successfully implemented swipe gesture animation.
But the problem is, when I click on next or previous button(oh yes, I also have next, previous, first and last buttons to read a book), swipe works perfectly.
But on webview it works weirdly. Following happens:
Swipe doesn't work on webview.
When I click on next or previous buttons and then swipe the webview, swipe on webview works.
Following is my code snippet:
In viewDidLoad:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionLeft];
[webViewPage addGestureRecognizer:swipeRight];
[_webview2 addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionRight];
[webViewPage addGestureRecognizer:swipeLeft];
[_webview2 addGestureRecognizer:swipeLeft];
To enable swipe on UIWebView:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// NSLog(#"shouldRecognizeSimultaneouslyWithGestureRecognizer");
return YES;
}
- (IBAction)btnPrevious_click:(id)sender {
//some code
}
- (IBAction)btnNext_click:(id)sender {
//some code
}
Where am I getting wrong?
From your code, you have to added 2 UISwipeGestureRecognizer in only one UIWebView name is _webview2 please change it as per your requirement.
Like iPatel mentioned. Each of the webview should has its own GestureRecognizers. So, you should have 4 GestureRecognizers in total.
Try this:-
UISwipeGestureRecognizer *swipeRight1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight1 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft1 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview1 addGestureRecognizer:swipeRight1];
[_webview1 addGestureRecognizer:swipeLeft1];
UISwipeGestureRecognizer *swipeRight2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
[swipeRight2 setDirection:UISwipeGestureRecognizerDirectionLeft];
UISwipeGestureRecognizer *swipeLeft2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[swipeLeft2 setDirection:UISwipeGestureRecognizerDirectionRight];
[_webview2 addGestureRecognizer:swipeRight2];
[_webview2 addGestureRecognizer:swipeLeft2];

Distinguish UISwipe gesture over NSTouches

I have a view where a user can draw things using his fingers on iPad.
He can use 1 - 4 fingers for drawing anything.
I am drawing with the help of NSTouches.
Now client wants me to add a swipe gesture to present a hidden menu.
Is this thing achievable and if yes then how ?
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)]; // MENTION WHAT YOU WANT TO DO IN rightSwipeHandle METHOD WHEN SWIPED RIGHT
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1]; // YOU CAN INCREASE THE NUMBER OF TOUCHES
//add the your gestureRecognizer , where to detect the touch..
[listView1 addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)]; // MENTION WHAT YOU WANT TO DO IN leftSwipeHandle METHOD WHEN SWIPED RIGHT
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[listView1 addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(upSwipeHandle:)]; // MENTION WHAT YOU WANT TO DO IN upSwipeHandle METHOD WHEN SWIPED RIGHT
upRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[upRecognizer setNumberOfTouchesRequired:1];
[listView1 addGestureRecognizer:upRecognizer];
[upRecognizer release];
UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(downSwipeHandle:)]; // MENTION WHAT YOU WANT TO DO IN downSwipeHandle METHOD WHEN SWIPED RIGHT
downRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[downRecognizer setNumberOfTouchesRequired:1];
[listView1 addGestureRecognizer:downRecognizer];
[downRecognizer release];

Ways to recognize UITouch for a UIButton

What are the different ways to recognize a UIButton being touched?
IBAction doesn't seem to be working with what I want, to my knowledge, I could probably use a gesture recognizer and check if the button's location was tapped, but is there any other way?
Try UITapGestureRecognizer.
//-- For identifing touch recognize
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(touchIdentifier:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[your_button addGestureRecognizer:singleTapGestureRecognizer];
Method for operation
-(void)touchIdentifier:(UITapGestureRecognizer*)gesture
{
NSLog(#" Current button ID %d \n",gesture.view.tag);
}
hello U can go with the UISwipe Recognier
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imgView_ addGestureRecognizer:swipeRight];
[swipeRight release];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imgView_ addGestureRecognizer:swipeLeft];
[swipeLeft release];
But DO NOT forget to set UserInteractionEnabled for the View

UISwipeGestureRecognizer blocked by UITapGestureRecognizer

I have a view which needs to handle pan, tap, and swipe gestures. I have pan and tap working, but when I add swipe, it doesn't work. Curiously, it seems that tap somehow blocks the swipes because if I remove the tap, then swipe works fine. Here's how I create my gesture recognizers.
- (void) initGestureHandlers
{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftGesture:)];
swipeLeftGesture.numberOfTouchesRequired = 1;
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRightGesture:)];
swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRightGesture];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[tapGesture setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
}
A lot of blog suggest using requireGestureRecognizerToFail to handle the case where tap fires before the double tap fires, so I tried that, but it also didn't work.
[tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
[tapGesture requireGestureRecognizerToFail:swipeRightGesture];
How can I get tap and swipe in the same view?
comment or remove the lines
//[tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
//[tapGesture requireGestureRecognizerToFail:swipeRightGesture];
Also set the all the guesture objects delegate to self. like this
- (void) initGestureHandlers
{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftGesture:)];
swipeLeftGesture.numberOfTouchesRequired = 1;
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeftGesture.delegate = self;
[self addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRightGesture:)];
swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture = self;
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRightGesture];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[tapGesture setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGesture];
tapGesture.delegate = self;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
panGesture.delegate = self;
}
Implement the delegate method like this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
put this one and try it,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
{
return YES;
}

UISwipeGestureRecognizer not responding to Up

I have the following UIGestureRecognizer which detects swipe up properly
- (void) addGestureRecognizer {
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
}
- (void) didSwipe:(id)sender{
NSLog(#"didSwipe");
}
I then modified the direction to include left and right
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
and it no longer responds to swiping up (swiping left or right works). What am I doing wrong? (tried on simulator, iphone5, ipad3)
Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe. Thanks.
Try this way
UISwipeGestureRecognizer *_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
_swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown];
[_swipeRecognizer setDelegate:self];
[self.view addGestureRecognizer:_swipeRecognizer];
EDIT
As #LearnCocos2D suggested " Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags. "
And As per your "Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe.", You just need to detect swipe not the direction so combining right-left as one and up-down as other gesture will work.
swipeGesture is discree gesture, and swipe up and down is two different gesture.
You must create 4 gesture and send it to one action.
- (void) addGestureRecognizer {
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeLeft setDelegate:self];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[swipeRight setDelegate:self];
[self.view addGestureRecognizer:swipeRight];
}
- (void) didSwipe:(UISwipeGestureRecognizer *)sender{
NSLog(#"didSwipe");
}

Resources