I'm using the J4n0 Callout code (github), to implement a custom annotation in MapKit.
In my annotation (MyCalloutView
) I'm using a button and a label.
When I'm clicking on my button, the methode handleTouch is called, but the sender correspond an UITapGestureRecognizer
with sender.view
always equal to my annotation view, and not the button.
MyCalloutView.h
@interface MyCalloutView : CalloutView
@property (nonatomic, retain) IBOutlet UILabel* title;
@property (weak, nonatomic) IBOutlet UIButton *clickButton;
- (IBAction) handleTouch:(id)sender;
- (id) initWithAnnotation:(CalloutAnnotation*)annotation;
- (IBAction)onClickButton:(id)sender;
@end
MyCalloutView.m
@implementation MyCalloutView
-(IBAction) handleTouch:(UITapGestureRecognizer *)sender {
//LogDebug(@"touch from : %@", sender);
UIButton *senderButton = (UIButton *)sender.view;
LogDebug(@"Sender class : %@ - Sender Tag : %d - Sender View class : %@", [sender class], sender.view.tag, sender.view.class);
LogDebug(@"Tap postion : (%f, %f)", [sender locationInView:sender.view].x, [sender locationInView:sender.view].y);
if(senderButton == self.clickButton){
LogDebug(@"le clique vient de click button !!");
}
}
[...]
CalloutView.h @class CalloutAnnotation;
@interface CalloutView : BaseCalloutView
- (IBAction) handleTouch:(id)sender;
- (id)initWithAnnotation:(CalloutAnnotation*)annotation;
@end
CalloutView.m @implementation CalloutView
-(IBAction) handleTouch:(id)sender {
LogDebug(@"touch %@", sender);
}
- (id)initWithAnnotation:(CalloutAnnotation*)annotation
{
NSString *identifier = NSStringFromClass([self class]);
self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
if (self!=nil){
[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
}
// prevent the tap and double tap from reaching views underneath
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch:)];
[self addGestureRecognizer:tapGestureRecognizer];
UITapGestureRecognizer *doubletapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch:)];
doubletapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubletapGestureRecognizer];
return self;
}
@end
This issue was a well known one. The solution is described in the GitHub : issue 1
The solution works for me.