I use the following code to handle 1 finger swipe in my code:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipe setDelaysTouchesBegan:YES];
[[self view] addGestureRecognizer:swipe];
I know i can add the following line to make it handle 2 fingers swipe:
[swipe setNumberOfTouchesRequired:2];
However when I add the above code 1 finger swipe is no longer detected since the number of touches required is now 2. What can I do to make my code work for 1, 2 or 3 fingers swipe?
I tried using the following code but this doesn't do what I want to do.
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:3];
[panRecognizer setDelaysTouchesBegan:YES];
[[self view] addGestureRecognizer:panRecognizer];
[panRecognizer release];
Thank you.
In your handleViewsSwipe you can get the numberOfTouches property from the gesture recognizer.
- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
NSUInteger touches = recognizer.numberOfTouches;
switch (touches) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
Just switch the same method for what to do depending on how many touches you get.
Add three swipe gesture recognizers to your view:
for (int i = 1; i <= 3; ++i) {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
swipe.numberOfTouchesRequired = i;
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.delaysTouchesBegan = YES;
[self.view addGestureRecognizer:swipe];
}
Worked for me.
Related
I am new to iPhone developer,
I made epub reader and loaded each page of epub in my webview
What i want to is, when user does right swipe gesture 2nd time then i want to navigate to new page, i do not want to do anything when user does right swipe gesture for first time.
UISwipeGestureRecognizer *swipeRight
Is there any method something like,
if(swipeRight.touch.count > 2)
{
// do this
}
Any help will be appriciated.
Thanks In Advance !
EDIT
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
if (scrollOffset == 0)
{
swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftAction:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.numberOfTouchesRequired=2;
swipeUp.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeUp.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeUp];
}
else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
{
swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightAction:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.numberOfTouchesRequired=2;
swipeDown.delegate = (id<UIGestureRecognizerDelegate>)self;
swipeDown.cancelsTouchesInView=YES;
[_webview addGestureRecognizer:swipeDown];
}
You can tell the UIWebView's UIScrollView that its UIPanGestureRecognizer should only fire when your own UISwipeGestureRecognizer has failed.
This is how you do it:
UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGesture:)];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGesture];
[self.view addGestureRecognizer:leftSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeGesture];
[_webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:leftSwipeGesture];
That should do the trick for you.
Just attach UIGestureRecognizer subclass to that view and hold on for calls...
UISwipeGestureRecognizer* rightSwipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(someAction)];
rightSwipeRecognizer.numberOfTouchesRequired = 2;
rightSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipeRecognizer.cancelsTouchesInView = YES;
[self.webView addGestureRecognizer:rightSwipeRecognizer]; // add in your webviewrightSwipeRecognizer
Try like below it will help you
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
I don't think that swipe gestures offer support for the kind of behavior you are aiming at, but you can easily accomplish it by doing the following:
on the first swipe, set a flag and start a timer; for the rest do nothing;
on the second swipe,
a. if the timer has fired (when firing, the timer reset the flag), do as per point 1.
b. is the timer has not fired (the flag is still set), then do you action and cancel the timer.
You might event think of defining a subclass of UISwipeGestureRecognizer to encapsulate all this behavior.
I have the following code which creates four gestures:
self.userInteractionEnabled = YES;
UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
[panGesture setDelegate:self];
[self addGestureRecognizer:panGesture];
UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPressGesture.minimumPressDuration = 0.00;
[self addGestureRecognizer:longPressGesture];
UISwipeGestureRecognizer * swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeUp:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer * swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipeDown];
The Pan and LongPress work fine, but I never get either of the Swipe gestures. Is there something special I need to do to have the swipe selectors get called?
Thanks
I just answered this yesterday.
Short form: a swipe gesture is a special case of a pan gesture, and by default no two gestures will recognize simultaneously. Look into gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: and/or requireGestureRecognizerToFail:. You'll find further help for this and related issues in Apple's guide.
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
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;
}
}
}
I'm trying to use swipe left and right on a UIScrollView. However it looks like swipe left does not work in iPhone simulator even though swipe right does. Did I miss any step?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.scrollView.multipleTouchEnabled = YES;
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipe.delaysTouchesBegan = YES;
swipe.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:swipe];
[swipe release];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
} else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
}
}
Use Following:
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
}
Your 'handleSwipe' code is actually correct. You need two UISwipeGestureRecognizers but you can point them both to the same handler, containing your 'IF' statement.
You can create one gesture recognizer that handles both left and right swipe (or even all directions-!):
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
All directions:
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;