If I add a down gesture recognizer then it does not work, if I change it to UISwipeGestureRecognizerDirectionRight then it works.
UISwipeGestureRecognizer *swipeDownRecognizer =
[[UISwipeGestureRecognizer alloc] initWithTarget:_delegate
action:#selector(statusViewDidDismiss:)];
swipeDownRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[swipeDownRecognizer setNumberOfTouchesRequired:1];
[self addGestureRecognizer:swipeDownRecognizer];
Related
I am trying to add a gesture recognizer using the following code.
However, the gesture is not getting recognized. Is there something else I have to do in order to make the view controller a delegate of the view--or something to that effect?
I have implemented the UIGestureRecognizerDelegate protocol in the .h file
//in view did load
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[_myImage addGestureRecognizer:rightRecognizer];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[_myImage addGestureRecognizer:leftRecognizer];
//new methods
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
NSLog(#"Right Swipe performed");//not appearing
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
NSLog(#"Left Swipe performed");//not appearing
}
1st you need
_myImage.userInteractionEnabled=YES;
then
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[_myImage addGestureRecognizer:swipeLeft];
[_myImage addGestureRecognizer:swipeRight];
then call method like
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
NSLog(#"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
NSLog(#"Right Swipe");
}
}
i hope this will work..
You have to enable user interaction of "_myImage" before adding gestures to image view as:
_myImage.userInteractionEnabled = YES;
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