我正在使用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