我正在使用 J4n0 标注代码 ( github ) 在 MapKit 中实现自定义注释。
在我的注释 ( MyCalloutView
) 中,我使用了一个按钮和一个标签。
当我单击我的按钮时,会调用方法 handleTouch,但发送者对应的UITapGestureRecognizer
总是sender.view
等于我的注释视图,而不是按钮。
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