I'm looking to add a gesture recognizer to UITableView that uses two finger up or down, like Twitterific has for toggling the status bar.
Here's my code
[self.tableView setMultipleTouchEnabled:YES];
[self.tableView.panGestureRecognizer setMaximumNumberOfTouches:1];
UISwipeGestureRecognizer *swipeUpGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doubleSwipeUp)];
[swipeUpGesture setNumberOfTouchesRequired:2];
[swipeUpGesture setDirection:UISwipeGestureRecognizerDirectionUp];
[swipeUpGesture setDelaysTouchesBegan:YES];
[self.tableView addGestureRecognizer:swipeUpGesture];
UISwipeGestureRecognizer *swipeDownGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doubleSwipeDown)];
[swipeDownGesture setNumberOfTouchesRequired:2];
[swipeDownGesture setDirection:UISwipeGestureRecognizerDirectionDown];
[swipeUpGesture setDelaysTouchesBegan:YES];
[self.tableView addGestureRecognizer:swipeDownGesture];
I'm calling this in viewDidLoad and doubleSwipeUp is not being called.
What can I do?
First I would check to make sure that self.tableView.multipleTouchEnabled is set to YES.
If that doesn't help then maybe try setting swipeUpGesture.delaysTouchesBegan = YES;
Related
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];
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];
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];
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
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");
}