0
我正在使用J4n0標註代碼(github)在MapKit中實現自定義註釋。MKAnnotationView自定義標註 - 處理觸摸按鈕
在我的註釋(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