6 different UISwipeGestureRecognizer, always receive the same one - ios

I have a TableView containing UITableViewCells, each with 6 buttons...
A UISwipeGestureRecognizer is added to each of the buttons like so:
- (void)awakeFromNib {
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_XXSButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_XSButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_SButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_MButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_LButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_XLButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_XXLButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
}
- (void)handleRightSwipe:(UIGestureRecognizer *)gestureRecognizer {
UIButton *button = (UIButton *)[gestureRecognizer view];
[self zeroButton:button];
}
When the swipe is recognised, it calls the method zeroButton .
No matter which button I do the swipe on, the view retrieved is of the first button. I would have expected [gestureRecognizer view] to return the button the swipe was performed on.
Any ideas why I always receive the gesture of the first button only?
I recognise that this code isn't the most elegant to start with, having to create 6 different UISwipeGestureRecognizer. I would have preferred to create the gesture on the UITableViewCell itself, and manually check if the swipe originated or passed over a particular button. Unfortunately, my attempt at this has proven unreliable. So if someone has a suggestion on how to implement that method instead would be even better
(I'll post that "solution" later)
Thanks...

One alternative approach, since you are using UIButtons would be using addTarget:action:forControlEvents: on your buttons for the events:
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
and the do your handling in the action method you specify.
It should not be difficult to "map" the branches within your gesture handling method to different methods (or just one method, if you like) inside of your class.
I understand this is not a completely satisfactory answer, since it gives up using gesture recognizers (and their facilities), but it could work for you.
As to why the gesture recognizer does not work correctly when applied to a UITableViewCell (and possibly it also explains why only the first button seems to receive the gesture), the behavior you described in your comment, reminded me that UIScrollView (base class of UITableView) is greedy with touches (not so much as a UIWebView, though) and that this for sure has an impact on how touches are forwarded. So, I don't know if it works, but I found this in UIScrollView:
delaysContentTouches
If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:. The default value is YES.
This could work or not, it could even make the UITableView not work at all, but it is easy to try out.
If this does not work and my previous hypothesis is still correct, a way around it would be subclassing UITableView and overriding the touch related methods (e.g., touchesShouldBegin:withEvent:inContentView:) so you can customize their behavior.
OLD and WRONG ANSWER:
It is my understanding the you can share one same gesture recognizer among different views, like this:
rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[_LButton addGestureRecognizer:rightSwipe];
[_SButton addGestureRecognizer:rightSwipe];
[_XButton addGestureRecognizer:rightSwipe];
[rightSwipe release];
I don't know if this can solve your issue, since what happens in your case is that, no matter which button you swipe, it is always that first gesture recognizer that gets activated and this is pretty hard to explain since you have different buttons. This could possibly be related to some overlapping (I am guessing) among the buttons, or to the fact that all of your buttons are "embedded" in a table cell, so the swipe mechanism at a cell level interferes with the swipe mechanism at the sub-cell level, but I am not sure about it.
As an alternative approach, which is surely working, you could attach your gesture recognizer to the very table cell (you mentioned your attempt to attach it to the table view itself; attaching to the table cell will work).
Hope this helps.

Actually, it was an error in the zeroButton code that caused it to always reset the first button. I used the tag assigned to the button to determine which action to create. My tags were incorrectly set! silly me. So the code in my original question was actually correct.
Since I have amended the code as follow:
- (void)awakeFromNib {
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleRightSwipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipe.delegate = self;
[self addGestureRecognizer:rightSwipe];
[rightSwipe release];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIButton class]])
{
// only recognises gesture started on a button
return YES;
}
return NO;
}
- (void)handleRightSwipe:(UIGestureRecognizer *)gestureRecognizer {
// Determine which button received the right swipe
// Get the position of the point tapped in the UIViewCell co-ordinate system
CGPoint tapPoint = [gestureRecognizer locationInView:self];
NSLog(#"%.1f %.1f", tapPoint.x , tapPoint.y);
UIView *viewAtBottomOfHierarchy = [self hitTest:tapPoint withEvent:nil];
if ([viewAtBottomOfHierarchy isKindOfClass:[UIButton class]])
{
[self zeroButton:(UIButton *)viewAtBottomOfHierarchy];
}
}
This looks at where the swipe originate and determine which button it started into (if any)...

Related

how can check that which uiimageview is presently clicked?

I have three UIImageview. I set the action for UIImageview by using tapGesture. I want each UIImageview to have a different action. How can I check that which UIImageview is presently clicked?
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTap setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTap];
[_imageTwo addGestureRecognizer:singleTap];
[_imageThree addGestureRecognizer:singleTap];
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
}
You can detect using below code:
-(void)singleTapping:(UITapGestureRecognizer *) recognizer{
if (recognizer.view == _imageOne){
} else if (recognizer.view == _imageTwo) {
}else
{
}
}
A UIGestureRecognizer is to be used with a single view. There is this explicit information in the Apple documentation:
Gesture Recognizers Are Attached to a View
Every gesture recognizer is associated with one view. By contrast, a
view can have multiple gesture recognizers, because a single view
might respond to many different gestures. For a gesture recognizer to
recognize touches that occur in a particular view, you must attach the
gesture recognizer to that view.
Firstly, you can't attach same UITapGestureRecognizer to more than one view.
So, your code will not work and you have to create three UITapGestureRecognizer instances and have to attach it to views like below:
UITapGestureRecognizer *singleTapImg1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg1 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg2 setNumberOfTapsRequired:1];
UITapGestureRecognizer *singleTapImg3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapping:)];
[singleTapImg3 setNumberOfTapsRequired:1];
[_imageOne addGestureRecognizer:singleTapImg1];
[_imageTwo addGestureRecognizer:singleTapImg2];
[_imageThree addGestureRecognizer:singleTapImg3];
Now, you can use below code to get, which imageview clicked and perform action according to that:
-(void)singleTapping:(UIGestureRecognizer *)recognizer {
if (recognizer.view == _imageOne){
//_imageOne tapped
}
else if (recognizer.view == _imageTwo) {
//_imageTwo tapped
}else{
//_imageThree tapped
}
}

Single Tap TapGestureRecognizer

I am trying to use this code for a tap gesture recognizer, it works fine when number of taps required is set to 2, but when I set the number of taps required to 1 it stop functioning. I appreciate any help in getting this to work.
UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapDetected:)];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[self.view addGestureRecognizer:doubleTap];
You try this:-
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector(tapDetected:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
This works for me.
If You only want the receiver to respond to one/first touch then setDelaysTouchesBegan:NO Since this is used for processing touches in UITouchPhaseBegan so it is analyzed and is prevented from being delivered. Property discussion from documentation:
When the value of this property is NO (the default), views analyze touch events in UITouchPhaseBegan and UITouchPhaseMoved in parallel with the receiver. When the value of the property is YES, the window suspends delivery of touch objects in the UITouchPhaseBegan phase to the view. If the gesture recognizer subsequently recognizes its gesture, these touch objects are discarded. If the gesture recognizer, however, does not recognize its gesture, the window delivers these objects to the view in a touchesBegan:withEvent: message (and possibly a follow-up touchesMoved:withEvent: message to inform it of the touches’ current locations). Set this property to YES to prevent views from processing any touches in the UITouchPhaseBegan phase that may be recognized as part of this gesture.
It looks to me like you are simply failing to set the delegate of your GestureRecognizer. Are you sure the double tap was firing successfully? When I placed your code into my project I see the same behavior but setting the delegate properly and using shouldRecognizeSimultaneouslyWithGestureRecognizer causes it to recognize the single tap properly.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapDetected:)];
[doubleTap setDelegate:self];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[picker addGestureRecognizer:doubleTap];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:UITapGestureRecognizer.class] &&
[otherGestureRecognizer isKindOfClass:UITapGestureRecognizer.class])
{
return YES;
}
else
{
return NO;
}
}

iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view

In my app, I have an image and a UITextView.
I have created a UITapGestureRecognizer for both the views but the issue is that wherever I click on the screen, only the method associated with the UITextView gets executed.
Even if I click on the image, only the UITapGestureRecognizer method associated with the UITextView gets executed.
Following is the code I've implemented:
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
[[self view] addGestureRecognizer:tapGestureRecognizer];
//The following are the methods associated
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(#"I am in handleTapFrom method");
}
- (void) handleTapFromImage: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(#"I am in handleTapFrom Image method");
[self.view makeToast:#"Your verification code does not match. Re-enter your verification code"];
}
I am sure I am missing something here.
The association in storyboard is correct to my knowledge.
Please correct me where I am going wrong
Thanks for your time
You should not add gesture on self.view.
It should get added on the view for which you want to identify tap event.
You are setting both the Tap Gesture Objects on [self view] object.
Also, the UIImageView object, lets call it imageObj, should have userInteractionEnabled = YES.
instead of:
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
you should do:
[imageObj setUserInteractionEnabled:YES];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
You generally use -addGestureRecognizer: on the object you want your gesture object to work on.
Say you have a UITapGestureRecognizer object called myTapGesture.
Then, to make it work...
on a UILabel *lblSomeObj it will be:
[lblSomeObj addGestureRecognizer:myTapGesture];
on a UIView *vwSomeObj it will be:
[vwSomeObj addGestureRecognizer:myTapGesture];
etc...
Just add the gesture in the respective views and not in self.view.
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
Add gesture on UIImageView object and make sure that image view userInteractionEnabled is set to YES
imageObj.userInteractionEnabled = YES;
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
You need to include this piece of code:-
[tapGestureRecognizerImage requireGestureRecognizerToFail:tapGestureRecognizer];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];

Add event to a programmatically generated UIView

So I have a method then when called generates a simple UIView with some labels in it:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 250)];
view1.backgroundColor = [UIColor redColor];
view1.userInteractionEnabled = YES;
[self.view addSubview:view1];
I call this method 6 times so it places 6 UIViews (I give them different coordinates of course) around the screen.
How can I detect when a user swipes right on one of them and then trigger some other method?
I've tried something like this:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myLabel1Tap)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[view1 addGestureRecognizer:swipeRight];
and then a method:
- (void)myLabel1Tap {
}
But I'm not sure what to do inside that method, how can I know which view was swiped if they are all called the same 'view1'?
change the gesture recognizers selector to accept an argument (by adding a colon after the method signature)
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myLabel1Swipe:)];
this will mean the gesture recognizer will get passed in, and then you can perform actions based on the gesture recognizers properties, e.g.
- (void)myLabel1Swipe:(UISwipeGestureRecognizer *)recogniser
{
UIView *swipedView = [recognizer view];
//do whatever you want with this view
}

Setting direction for UISwipeGestureRecognizer

I want to add simple swipe gesture recognition to my view based iPhone project. Gestures in all directions (right, down, left, up) should be recognized.
It is stated in the docs for UISwipeGestureRecognizer:
You may specify multiple directions by specifying multiple UISwipeGestureRecognizerDirection constants using bitwise-OR operands. The default direction is UISwipeGestureRecognizerDirectionRight.
However for me it doesn't work. When all four directions are OR'ed only left and right swipes are recognized.
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
[super viewDidLoad];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
}
I fixed this with adding four recognizers to the view but I'm curious to know why didn't it work as advertised in docs?
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
[super viewDidLoad];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
}
Seems like there is a bug. You can specify the allowed direction(s) as you did. But when you try to access the actual direction that triggered the swipe in the action selector method you still get the bit mask you originally set (for the allowed directions).
This means that checks for the actual direction will always fail when more than 1 direction is allowed.
You can see it for yourself quite easily when you output the value of 'direction' in the selector method (ie -(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer).
Filed a bug report (#8276386) to Apple.
[Update] I got an answer from Apple saying that the behavior works as was intended.
So for example in a table view you can swipe left or right in a table view cell to trigger 'delete' (this would have directions of the swipe gesture set to left and right)
This means that the original workaround is the way it's supposed to be used. The direction property can only be used to get the gestures recognized correctly, but not in the method performed on a successful recognition to compare for the actual direction that triggered the recognition.
I noticed that left/right and up/down gestures work together in pairs, so you only need to specify two gesture recognizers. And the docs do seem to be wrong.
Well that sucks, I solved the problem by adding 2 gestures like Lars mentioned and that worked perfectly...
1) Left/Right
2) Up/Down
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:swipeLeftRight];
UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
[self.view addGestureRecognizer:swipeUpDown];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
Now this is the didSwipe function
- (void) didSwipe:(UISwipeGestureRecognizer *)recognizer{
if([recognizer direction] == UISwipeGestureRecognizerDirectionLeft){
//Swipe from right to left
//Do your functions here
}else{
//Swipe from left to right
//Do your functions here
}
}
If your using Xcode 4.2 you can add Gesture Recognizers # the storyboard and then link the GUI Gesture Recognizers to IBActions.
You can find the Gesture Recognizers in the Object Library of the Utility Pane (The bottom of the Right pane).
Then its just a matter of Control-dragging to the appropriate action.
If you want it to detect all four directions, you'll need to create four instances, as you did in your work-around.
Here's Why:
The same instance of UISwipeGestureRecognizer that you create is the instance that gets passed to the selector as sender. So if you set it to recognize all four directions, it will return true for sgr.direction == xxx where xxx is any one of the four directions.
Here's an alternative work-around that involves less code (assumes ARC use):
for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
sgr.direction = d;
[self.view addGestureRecognizer:sgr];
}
Swift 2.1
I had to use the following
for var x in [
UISwipeGestureRecognizerDirection.Left,
UISwipeGestureRecognizerDirection.Right,
UISwipeGestureRecognizerDirection.Up,
UISwipeGestureRecognizerDirection.Down
] {
let r = UISwipeGestureRecognizer(target: self, action: "swipe:")
r.direction = x
self.view.addGestureRecognizer(r)
}
Here is a code sample for UISwipeGestureRecognizer usage. Note comments.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//add gesture recognizer. The 'direction' property of UISwipeGestureRecognizer only sets the allowable directions. It does not return to the user the direction that was actaully swiped. Must set up separate gesture recognizers to handle the specific directions for which I want an outcome.
UISwipeGestureRecognizer *gestureRight;
UISwipeGestureRecognizer *gestureLeft;
gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];//direction is set by default.
gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];//need to set direction.
[gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
//[gesture setNumberOfTouchesRequired:1];//default is 1
[[self view] addGestureRecognizer:gestureRight];//this gets things rolling.
[[self view] addGestureRecognizer:gestureLeft];//this gets things rolling.
}
swipeRight and swipeLeft are methods that you use to perform specific activities based on left or right swiping. For example:
- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
NSLog(#"Right Swipe received.");//Lets you know this method was called by gesture recognizer.
NSLog(#"Direction is: %i", gesture.direction);//Lets you know the numeric value of the gesture direction for confirmation (1=right).
//only interested in gesture if gesture state == changed or ended (From Paul Hegarty # standford U
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
//do something for a right swipe gesture.
}
}
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleGestureNext:)];
Updown.delegate=self;
[Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[overLayView addGestureRecognizer:Updown];
UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleGestureNext:)];
LeftRight.delegate=self;
[LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
[overLayView addGestureRecognizer:LeftRight];
overLayView.userInteractionEnabled=NO;
-(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"Swipe Recevied");
//Left
//Right
//Top
//Bottom
}
hmm, strange, it works perfect for me, I do exactly same thing
think you should try look at
UIGestureRecognizerDelegate method
- (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gestureRecognizer {
// also try to look what's wrong with gesture
NSLog(#"should began gesture %#", gestureRecognizer);
return YES;
}
in logs you must see something like:
should began gesture ; target= <(action=actionForUpDownSwipeGestureRecognizer:, target=)>; direction = up,down,left,right>
use this, it should be the bit operation
gesture.direction & UISwipeGestureRecognizerDirectionUp ||
gesture.direction & UISwipeGestureRecognizerDirectionDown
This was driving me crazy. I finally figured out a reliable way to have multiple swipeGestureRecognizers.
It appears there is a bug in iOS if the name of your "action" selector is the same across multiple swipeGestureRecognizers. If you just name them differently, e.g. handleLeftSwipeFrom and handleRightSwipeFrom, everything works.
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];

Resources