Add gesture recogniser to navigation bar - ios

I'm trying to add a pan gesture recogniser on the navigation bar:
[self.navigationController.navigationBar addGestureRecognizer:self.panGestureRecognizer];
but it doesn't seem to trigger the gesture. Any ideas why?

first create pangesture object.
add gesture recognizer to navigationbar view.
then call your gesture recognizer method.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(pushVC:)];
[self.nav.view addGestureRecognizer:panGesture];
-(void)pushVC:(UIPanGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateEnded)
{
pushVC *pushvc = [[pushVC alloc] initWithNibName:#"pushVC" bundle:nil];
[self.nav pushViewController:pushvc animated:YES];
}
}

Related

UISwipeGestureRecognizer UIGestureRecognizerStateChanged not called

I'm define two UISwipeGestureRecognizer in my application:
UISwipeGestureRecognizer *swipeLeftVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureVolume:)];
swipeLeftVolume.direction = UISwipeGestureRecognizerDirectionLeft;
[self.playerView addGestureRecognizer:swipeLeftVolume];
UISwipeGestureRecognizer *swipeRightVolume = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureVolume:)];
swipeRightVolume.direction = UISwipeGestureRecognizerDirectionRight;
[self.playerView addGestureRecognizer:swipeRightVolume];
In the target method i have 3 states:
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
UIGestureRecognizerStateEnded
and i noticed that only the UIGestureRecognizerStateEnded is called.
Any idea what can be the problem? i want to recognize a swipe on specific UIView :
- (void)handleSwipeGestureVolume:(UISwipeGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(#"Start");
} else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(#"Changed");
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(#"Finish");
}
}
Seem like you are confused between UISwipeGestureRecognizer and UIPanGestureRecognizer.
UISwipeGestureRecognizer will generate UIGestureRecognizerStateEnded state only while UIPanGestureRecognizer has both 3 states you want.
If you need to receive both UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded, use UIPanGestureRecognizer instead.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureVolume:)];
[self.playerView addGestureRecognizer:panGesture];
Make sure you enable UserInteraction for self.playerView.
For any gesture recogniser, user interaction must be enabled to work. Without it none of the gestures will be triggered including touchBegan:, touchMove:, etc.
so you need to make [self.playerView setUserInteractionEnabled:TRUE];
And one more thing I would like to let you know that if you have implemented touchBegan:, touchMove:, etc. then the method will be triggered first for gesture rather than UIGestureRecognizer shilds.

swipe with one finger and two finger swipe on the same UIView subclass

I have a custom implementation of UITableViewCell.
The UITableViewCell can be swiped to the left or the right.
I use a UIPanGestureRecognizer for the same.
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
}
#pragma mark - horizontal pan gesture methods
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// if the gesture has just started, record the current centre location
// SOME CODE
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
// translate the center
//SOME CODE
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
//SOME CODE
}
}
Now I want to be able to support two finger swipe on on the entire screen such that even if a two finger swipe is done on a UITableViewCell, the above code is not triggered.
How can I achieve this ?
If you set the maximumNumberOfTouches on your gesture recognizer to 1, it will no longer accept multi-finger swipes:
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
recognizer.maximumNumberOfTouches = 1;

gesture recognizers on both UITableViewcell and UITableVIew

I have a cutsom UITableViewCell implemention.
I have registered this subclass of UITableViewCell for a UIPanGestureRecognizer which i use to swiping the cells to the right or left.
// in the UITableViewCell subclass :
UIGestureRecognizer* recognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:
self
action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
recognizer.cancelsTouchesInView = NO;
Now I want to to present a view controller when the user does a two finger swipe "up"
on the screen.
So, I added a UISwipeGestureRecognizer to the tableview.
// code in the view controller containing the tableview reference.
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionUp];
[swipe setDelaysTouchesBegan:NO];
[[self tableView ]addGestureRecognizer:swipe];
swipe.cancelsTouchesInView= YES;
[swipe setNumberOfTouchesRequired:2];
swipe.delegate = self;
self.tableView.multipleTouchEnabled = YES;
But when I do a two finger swipe on the screen, the pan gesture gets triggered .
How can I solve this ?
As sooper's says, setting the maximumNumberOfTouches = 1 will probably work.
For others trying to deal with 2 gestureRecognizers at the same time that are both 1 touch gestures I found that making sure to set this delegate to yes
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
and then in the gesture recognizer action you can check for a certain translation or whatever you need and cancel one of the gesture recognizers.
Such as:
- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.superview];
if(panRecognizer.state == UIGestureRecognizerStateBegan)
{
if(fabsf(translation.x) < fabsf(translation.y))
{
//deactivate horizontal gesture recognizer
panRecognizer.enabled = NO;
panRecognizer.enabled = YES;
}
else //if(fabsf(translation.x) > fabsf(translation.y))
{
//deactivate vertical gesture recognizer
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
}
//other statements like stateChanged and stateBegan
}

How to implement swipe gesture on half view and pan gesture on other half view?

How to add a pan gesture to first half (0,0,160,480) of a view and on same view a swipe gesture on (160,0,160,480) as well?
The view is table view and on swiping left the cell content should change and on panning in right the tableview should move like facebook ios app
Take two Different UIview as SubView of mainView with Required Frame Size then add Desired Gestures :-
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeVC:)];
swipeGestureRecognizer.delegate = self;
[self.subView.view addGestureRecognizer:swipeGestureRecognizer];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panVC:)];
panGestureRecognizer.delegate = self;
[panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
[self.subView2.view addGestureRecognizer:panGestureRecognizer];
The Below method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

Forward pan/pinch gesture to mkmapview

I have a UIViewController with a MKMapView and an other View (lets say PaintView) on top of the mapView.
When a user touches the PaintView with a pinch gesture, the mapview underneath should respond (as well as the paintView self).
When a user touches the Paintview with a pan gesture the mapview must not respond. (the pan gesture is used for painting).
Is there any possibility to forward the pinch gesture to the mapView?
[self.view addSubview:self.mapView];
[self.view addSubview:self.paintView];
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[self.paintView addGestureRecognizer:panRecognizer];
panRecognizer.delegate=self;
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[self.paintView addGestureRecognizer:pinchGesture];
pinchGesture.delegate=self;
-(void)pan:(UIPanGestureRecognizer *)gesture
{
//do sth in the paintView
}
-(void)pinch:(UIPinchGestureRecognizer *)gesture
{
//forward pinch gesture to mapview
//do sth in paintview
}

Resources