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
}
Related
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];
}
}
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
}
I have tried so many thing to get touch inside a UIScrollView but I'm not getting how to do it.
Actually I want to touch inside a UIScrollView and get the location of subview (which i have touched).
You can do it with gesture recognizers. For detect single tap location use UITapGestureRecognizer
UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[myScrollView addGestureRecognizer:tapRecognizer];
- (void)tapAction:(UITapGestureRecognizer*)sender{
CGPoint tapPoint = [sender locationInView:myScrollView];
CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view];
}
To convert that tapPoint to self.view you can use convertPoint:toView: method in UIView class
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;
}
I have the following problem with a UIPanGestureRecognizer inside a UIScrollView:
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
sv.contentSize = CGSizeMake(200, 100 *100);
for (int i = 0; i < 100; i++) {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, i * 100, 200, 100)];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panTile:)];
[panGesture setDelegate:self];
[panGesture setEnabled:FALSE];
[newView addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[newView addGestureRecognizer:longPressRecognizer];
[sv addSubview:newView];
}
The complete scroll view is filled with small tiles, each of them implements a pan gesture in order to make them draggable. The problem is that - doing so - prevents the scroll view from scrolling. Dragging of the tiles instead works fine. When I disable the tiles pan gestures, scrolling works perfectly. The tiles pan gesture somewhat hides the scroll views own pan gesture. My idea was to disable the tiles pan gesture from the beginning. The gesture is enabled once the user does a long press on the tiles. The problem is that the user has to lift the finger and then touch the tile again to drag it. When the drag is finished, I enable the long press and disable the pan gesture again. So longPress: looks as follows:
- (void)longPress:(UILongPressGestureRecognizer *) gestureRecognizer {
for (UIGestureRecognizer *r in gestureRecognizer.view.gestureRecognizers) {
if ([r isKindOfClass:[UIPanGestureRecognizer class]]) {
[r setEnabled:TRUE];
}
}
//pan gesture should take over here...
}
Is there any possibility to glue the long press and the pan gestures together so that the user doesn't have to lift the finger? Or maybe another solution?