In viewDidLoad I set:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:swipeGesture];
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
UISwipeGestureRecognizerDirection temp = recognizer.direction;
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
[self backCalendarPressed:nil];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
[self nextCalendarPressed:nil];
}
}
but recognizer.direction is always equal to '3'.
And that's why I can't determine if it's left or right swipe.
you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions).
You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.
You should try this. It is working fine.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(backCalendarPressed:)];
[swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];
swipeLeft=nil;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextCalendarPressed:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
Basically you cannot set multiple directions to a single Swipe gesture recognizer. Use separate gesture recognizers for Left and Right directions.
try this:
The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.
The easiest would be to use two separate gesture recognizers instead.
If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];
-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
[self backCalendarPressed:nil];
}
-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
[self nextCalendarPressed:nil];
}
You should implement all the UIGestureRecognizer states in this handler method. Thus you can handle all the cases. I am stating this because I suspect the gesture recognizer has not yet properly recognized it's gesture. Try looking for the direction in the UIGestureRecognizerStateChanged case.
Related
I am using :
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewClicked:)];
[tmp addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
to get notification when a view(bigview for our example) i clicked(I have a lot of view), and in the front there is a UIView (a blank one) the he is in the front of the view (there is a reason why this view is in the front before al the views).
there is now a problem to get notification when tmp is tapped because the bigview is in the front.
there is any solution for something like this?
EDIT
In the bigview i have UISwipeGestureRecognizer:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[itemsView addGestureRecognizer:recognizer];
[recognizer release];
and if i make userInteractionEnabled in the bigview to NO he don't get notification on swipe
Two options:
A. Set userInteractionEnabled to NO on that big/blank view.
B. Implement pointInside:withEvent: on that big/blank view
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// UIView will be "transparent" for touch events if we return NO
return NO;
}
(as taken from allowing-interaction-with-a-uiview-under-another-uiview)
Actually i want to implement swipe left and right in UIScrollview. i have scrollview with content size (768,1500). i have tried this but problem is that sometimes its not detecting swipe and perform scrolling there. so now i want to disable scrolling on 2 finger touch.
swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipeGesture];
swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(previousswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeGesture];
i have tried custom scrollview for that but i have problem with touchesBegan method. its not calling every time. even i tried this but not able to stop two finger scroll in UIScrollview.
for (UIGestureRecognizer *mgestureRecognizer in _scrollView.gestureRecognizers) {
if ([mgestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer;
mpanGR.minimumNumberOfTouches = 1;
mpanGR.maximumNumberOfTouches = 1;
}
}
Let me know if you have any solution or alternative for that.
I had the same problem; I needed to disable two-finger scrolling so that I could detect a two-finger swipe to the left or right. Here's what I did to set up my scroll view:
- (void) setUpGestureHandlersOnScrollView:(UIScrollView *)scrollView {
// set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view
// we initialize without a target or action because we don't want the two-finger pan to be handled
UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init];
twoFingerPan.minimumNumberOfTouches = 2;
twoFingerPan.maximumNumberOfTouches = 2;
[scrollView addGestureRecognizer:twoFingerPan];
// set up the two-finger left and right swipe recognizers
UISwipeGestureRecognizer *twoFingerSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureFrom:)];
twoFingerSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
twoFingerSwipeLeft.numberOfTouchesRequired = 2;
[scrollView addGestureRecognizer:twoFingerSwipeLeft];
UISwipeGestureRecognizer *twoFingerSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureFrom:)];
twoFingerSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
twoFingerSwipeRight.numberOfTouchesRequired = 2;
[scrollView addGestureRecognizer:twoFingerSwipeRight];
// prevent the two-finger pan recognizer from stealing the two-finger swipe gestures
// this is essential for the swipe recognizers to work
[twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeLeft];
[twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeRight];
}
The handler method should look something like this:
- (void)handleGestureFrom:(UISwipeGestureRecognizer *)recognizer {
if ([recognizer numberOfTouches] == 2) {
// do whatever you need to do
}
}
You can create two gesture recognizers, one for single tap and one for double tap:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTouchesOne:)];
singleTapGesture.cancelsTouchesInView = NO;
singleTapGesture.delaysTouchesEnded = NO;
singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
singleTapGesture.numberOfTapsRequired = 1;
[Scroll_view addGestureRecognizer:singleTapGesture];
[singleTapGesture release];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTouchesTwo:)];
doubleTapGesture.cancelsTouchesInView = NO;
doubleTapGesture.delaysTouchesEnded = NO;
doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
doubleTapGesture.numberOfTapsRequired = 2;
[Scroll_view addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
And then, here comes the punch:
[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];
requireGestureRecognizerToFail Reference
The last line, makes your single tap handler work only if the double tap fails. So, you get both single tap and double tap in your application.
And So you can do like, in "doubleTapGesture" method you just specified the scrollview with content size (0,0), In "singleTapGesture" method you specified the scrollview with content size (768,1500).
Source knowledge
I added this code in cellForRowAtIndexPath
UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipeFrom:)];
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:gestureR];
it works fine. But I want UISwipeGestureRecognizerDirectionLeft so Added like this
[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];
When I check with direction and state I am always getting 3 = 3
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"%d = %d",recognizer.direction,recognizer.state);
}
if I apply only one Gesture it works fine. I tried to add two gestures one by one. but it will responding for only one gesture.
How to add second gestures. I added directly to one gesture to TableView another one to cell but now use.
Try this
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:gestureR];
If you want to handle different functionalities on left and right swipes, just change the selectors.
Instead of two times alloc, it would be better if you use
UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];
And get the direction of swipe in the action as :
-(void)handleSwipe:(UISwipeGestureRecognizer *) sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
//do something
}
else //if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
//do something
}
}
I know its been ages since you asked this.
But try and read following line again in your question.
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];
Did you realise you added UISwipeGestureRecognizerDirectionRight. Twice!!
:D
How can I add a UIGestureRecognizer to recognise left or right swipes on top of a UIImageView? I just need an action to be called when the swipe is left and another action for when the swipe is right.
Create 2 swipe gestures and add them to your image view.
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myRightAction)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[myImageView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer * recognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myLeftAction)];
[recognizer2 setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[myImageView addGestureRecognizer:recognizer2];
[recognizer2 release];
Have you set
#property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled
to true before you attach the UIGestureRecognizer?
If not try that
If that doesn't work, you could add a temp view and add the gesture recognizer to the temp view and then add the imageview to the temp view
I have a CCSprite that I want to move around using gestures. Problem is I'm completely new to Cocos2D. I want my sprite to perform one action when the gesture is up, another one when gesture is down, another action when gesture is right and same thing for left. Can someone point me in the right direction?
Thanks!
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.
The solution is to add one UISwipeGestureRecognizer for each direction you want the swipe gesture to be recognized, and set each recognizer's direction accordingly to either up, down, left and right. If you want to test for a swipe in any direction you'll have to add four UISwipeGestureRecognizers.
It's kind of odd but that's the only way it worked for me.
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
[self.gestureAreaView addGestureRecognizer:swipeGesture];
[swipeGesture release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can't be recognized direction
}
or
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];
UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];
[swipeGesture2 release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{
}
}
Use a UIPanGestureRecogizer and detect the swipe directions you care about. see the UIPanGestureRecognizer documentation for details. -rrh
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panRecognized:)];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
-(void)panRecognized:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
// you might want to do something at the start of the pan
}
CGPoint distance = [sender translationInView:self]; // get distance of pan/swipe in the view in which the gesture recognizer was added
CGPoint velocity = [sender velocityInView:self]; // get velocity of pan/swipe in the view in which the gesture recognizer was added
float usersSwipeSpeed = abs(velocity.x); // use this if you need to move an object at a speed that matches the users swipe speed
NSLog(#"swipe speed:%f", usersSwipeSpeed);
if (sender.state == UIGestureRecognizerStateEnded) {
[sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
if (distance.x > 0) { // right
NSLog(#"user swiped right");
} else if (distance.x < 0) { //left
NSLog(#"user swiped left");
}
if (distance.y > 0) { // down
NSLog(#"user swiped down");
} else if (distance.y < 0) { //up
NSLog(#"user swiped up");
}
// Note: if you don't want both axis directions to be triggered (i.e. up and right) you can add a tolerence instead of checking the distance against 0 you could check for greater and less than 50 or 100, etc.
}
}
The defaut direction is UISwipeGestureRecognizerDirectionRight. the multiple directions also can be specified like that :
[swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];
///
But if you want to get every single direction ,like that:
UISwipeGestureRecognizer *swipeGestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureRight:)];
[swipeGestureR setDirection: UISwipeGestureRecognizerDirectionRight ];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureR];
[swipeGestureR release];
UISwipeGestureRecognizer *swipeGestureL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureLeft:)];
[swipeGestureL setDirection: UISwipeGestureRecognizerDirectionLeft];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureL];
[swipeGestureL release];
the function handleSwipeGestureLeft will be called when swipe to left,and handleSwipeGestureRight wil be called when you swipe to right
Add one UISwipeGestureRecognizer for each axe (horizontal and vertical):
UISwipeGestureRecognizer *horizontalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(action)];
[horizontalSwipe setDirection:(UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:horizontalSwipe];
UISwipeGestureRecognizer *verticalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(action)];
[verticalSwipe setDirection:(UISwipeGestureRecognizerDirectionUp |
UISwipeGestureRecognizerDirectionDown )];
[self.view addGestureRecognizer:verticalSwipe];
-(void)addGesture {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender {
if (sender.direction == UISwipeGestureRecognizerDirectionUp) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
//do something
}
}
Can also use a switch instead of all the if statements
Even though there's lots of good info here, I couldn't find a quick answer that had it all.
If you want to differentiate whether a swipe is left or right or up or down, you need to create a new UISwipeGestureRecognizer for each direction.
However! This is not so bad, because you can route each of your gesture recognizers to the same selector, that can then use a switch statement as you might expect.
First, add gesture recognizers for each direction and route them to the same selector:
- (void)setupSwipeGestureRecognizers
{
UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(userDidSwipeScreen:)];
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(userDidSwipeScreen:)];
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGestureRecognizer];
[self.view addGestureRecognizer:leftSwipeGestureRecognizer];
}
Second, differentiate between the directions with a switch statement:
- (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
switch (swipeGestureRecognizer.direction) {
case UISwipeGestureRecognizerDirectionLeft: {
// Handle left
break;
}
case UISwipeGestureRecognizerDirectionRight: {
// Handle right
break;
}
default: {
break;
}
}
}