UITapGestureRecognizer on MKMapView breaks MKAnnotation selection - objective-c

I've added a UITapGestureRecognizer to an MKMapView, like so:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doStuff:)];
[tapGesture setCancelsTouchesInView:NO];
[tapGesture setDelaysTouchesEnded:NO];
[[self myMap] addGestureRecognizer:tapGesture];
[tapGesture release];
This almost works: tap gestures are recognized and double taps still zoom the map. Unfortunately, the UITapGestureRecognizer interferes with the selection and deselection of MKAnnotationView elements, which are also triggered by tap gestures.
Setting the setCancelsTouchesInView and setDelaysTouchesEnded properties doesn't have any effect. Annotation selection works fine if I don't add the UIGestureRecognizer.
What am I missing?
UPDATE:
As suggested by Anna Karenina below, this problem can be avoided by returning YES in the shouldRecognizeSimultaneouslyWithGestureRecognizer: delegate method.
More details in this answer.

Instead of tap gesture, add long press gesture as below :-
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(longpressToGetLocation:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D location =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
}

Related

Having issue with number of taps in UITapGestureRecognizer

I am trying to run a method by 2 times tapping on tv remote, consider tapping not clicking, but the touch surface does't recognize the taps. Instead, clicking two times runs the doubleTapping method.
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
doubleTap.allowedTouchTypes =#[[NSNumber numberWithInteger:UITouchTypeIndirect]];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
- (void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// handling code
NSLog(#"2 times");
}
}
I am missing something?
I was forgot to mention the UIPressType value , now due to position of remote's surface (Up / Down / Right / Left) you can now detect user's tap direction and add numberOfTapsRequired to the action :
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tapGestureRecognizer setAllowedPressTypes:#[#(UIPressTypeLeftArrow)]];
[tapGestureRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapGestureRecognizer];

How to detect the location of taps in ios 7

I have a view controller with half table view (bottom, 320x289)and half map view (top, 320,289). How can I detect location of tap?
Currently my code for the tap looks like this - when tapping, it hides the navigation bar so that the map gets some extra real estate. However, because it's not detecting location of the tap, when I tap on the tableview, I'm not able to segue into my table view controller.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideShowNavigation)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
Ideally I would like to detect location of taps. If tapped at the top (if height <=289px), it hides navigation bar (or maybe even segue into a separate view controller where map is full screen). If tapped at the bottom (if height > 289px), then it pushes the segue into table view controller.
- (void) hideShowNavigation:(id)sender
{
[self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden];
[self hidesBottomBarWhenPushed];
}
Here's the whole code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = YES;
self.automaticallyAdjustsScrollViewInsets = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideShowNavigation:)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
}
- (void) hideShowNavigation:(id)sender
{
CGPoint = [sender locationInView:self.view];
CGFloat y = location.y;
if(y<=289){
[self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden];
[self hidesBottomBarWhenPushed];
}
}
In your selector:
CGPoint location = [sender locationInView:self.view];
CGFloat x = location.x;
CGFloat y = location.y;

UIBarButtonItem and UIGestureRecognizer

I have a UIView where i added a UITapGestureRecognizer:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired=1;
tapRecognizer.numberOfTouchesRequired=1;
[self.myView addGestureRecognizer:tapRecognizer];
I then add a UIToolBar with a button to the view:
UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, 44)];
topBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered target:self action:#selector(logout)];
[topBar setItems:#[logout] animated:NO];
I'm having an issue where I click on the logout button, and my tap recognier fires instead of my logout action. If I click and hold, then the logout action will fire (I'm guessing the tap recognizer is failing so lets the buttion action fire).
how can I not fire the gesture recognizer when the button is pressed?
Just had the same problem. Because I don't want to introduce container views (the UIToolbar should cover my existing view). With the help of Patrick.Ji's coarsely pointing I came up with this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}
Don't forget to set the delegate of the gesture to self
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *mainTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mainTapGesture:)];
mainTapGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:mainTapGestureRecognizer];
}
Check the view in your tap recognizer. If it is your logout button, let the touch fail to pass it up the chain via super.
Alternatively, make sure your toolbar is not a subview of your view. Instead, have a container view containing with your toolbar and your content view, and add the gesture recognizer to this content view.
implement this delegate method of UIGestureRecognizer (remember to set your tapRecognizer.delegate = self)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch {
if ([touch.view isKindOfClass:[UIBarButtonItem class]])
{
return NO;
}
return YES;
}

Tap Recognition for UIImageView in the UITableViewCell

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.
Question: How could I do it? Could any one show me the code, or any tutorial?
Thanks in advance!
This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapMethod:)];
[self.imageView addGestureRecognizer:tap];
[self.imageView setUserInteractionEnabled:YES];
}
return self;
}
- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
UIImageView *imageView = (UIImageView *)tapGesture.view;
NSLog(#"%#", imageView);
}
Try this
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
{
RKLogDebug(#"imaged tab");
}
make sure u have....
imageView.userInteractionEnabled = YES;
you can use a customButton instead UIImageView
A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.
Use ALActionBlocks to action in block
__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
NSLog(#"pan %#", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];

Double tapping problem

Im working on an app with zooming function.
In this app I have this button. I want it to respond to tapping in several ways:
Single tap: Zoom in slightly.
Double tap: Zoom in to the max.
Ive tried several options to achieve this but none are what I want.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[zoomin addTarget:self action:#selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];
Both work on single and double tap but when I press the button once to slightly zoom and seconds later I press it again it doesn't zoom in slightly, it zooms in to the max.
It is possible to fix this with a timer and location check so that when u tap and tap again u can be sure that the location is in a similar area and the taps happened within timer range.
But is this what I really need?
Is there a simpler solution?
the solution provided by omz is not good.
where as you can do this by simply adding these lines of code like posted here. Double-tap or two single-taps?
NOTE THE MAGICAL LINE : [tapRecg requireGestureRecognizerToFail:doubleTapRecg];
ABSTRACT:
UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(doubleTapped:)];
doubleTapRecg.delegate = self;
doubleTapRecg.numberOfTapsRequired = 2;
doubleTapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:doubleTapRecg];
UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tapped:)];
tapRecg.delegate = self;
tapRecg.numberOfTapsRequired = 1;
tapRecg.numberOfTouchesRequired = 1;
[view addGestureRecognizer:tapRecg];
[tapRecg requireGestureRecognizerToFail:doubleTapRecg];
[doubleTapRecg release];
[tapRecg release];
You can do it with two gesture recognizers and a timer:
UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];
You'll have to use a slight delay in your tap: action before zooming in slightly because the first tap could be followed by a second tap:
- (void)tap:(UITapGestureRecognizer *)recognizer
{
[self performSelector:#selector(singleTap) withObject:nil afterDelay:0.25];
}
- (void)singleTap
{
//slightly zoom in...
}
- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
//Cancel the timer for the single tap action:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(singleTap) object:nil];
//zoom in to the max zoom level...
}