I have a map with annotations where I am performing the following actions:
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view{
[self methodA];}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
[self methodB];}
Both method A and B remove or add views in mapView.superview.
All works fine when dealing with one annotation. The Problem is when I have more than one annotation and I select two, one after the other.
If I click anywhere else in the map everything works fine. But when I click in the second annotationview after the first one, it performs "didDeselectAnnotationView" and then "didSelectAnnotationView" which calls both methods A and B, and it is not what I want. I would like it to detect that I am clicking in another annotation and ignore both methods.
I have researched about this but haven't found any solution yet.
I have tried to add an global variable and play with it as well as identifying where the user touches:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
// Get the specific point that was touched
CGPoint point = [touch locationInView:self.mapView];
for (id<MKAnnotation> annotation in self.mapView.annotations) {
MKAnnotationView* anView = [self.mapView viewForAnnotation: annotation];
if (anView){
CGRect frame = CGRectMake(anView.frame.origin.x-5, anView.frame.origin.y-5, anView.frame.size.width+5, anView.frame.size.height+5);
if ( CGRectContainsPoint(frame, point) ) {
NSLog(@"BELONGS");
}
}
}}
However, this didn't catch all the touches plus it would be a bit of a spaghetti solution.
Any ideas how to solve this?
All the best
The delegate doesn't do any distinction between the case where you unselect and then select another annotation view and the case where you select a new annotation view while one is already selected. In both cases, these calls will be done in that order:
You want 2 things :
1) not call methodB when selection of view2 is very close (in time) to a deselection of view1.
2) not call methodA when a selection is close (in time) to a previous selection.
To resolve those two problems, you can store two timestamps: 1) last selection 2) last deselection. With these two variables you can set a threshold to define what "close" mean in the two rules above.
Now you have a condition for preventing the call of methodA and methodB in the specific case of a fast deselection and selection (which is what really happens for the delegate when you switch from one selection to another one).