MKAnnotationView自定义标注 - 处理按钮上的触摸

时间:2012-09-11 15:00:29

标签: objective-c mkannotationview mapkit

我正在使用J4n0标注代码(github),在MapKit中实现自定义注释。

在我的注释(MyCalloutView)中,我正在使用按钮和标签。

当我点击我的按钮时,调用了方法handleTouch,但是发件人对应UITapGestureRecognizersender.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

1 个答案:

答案 0 :(得分:0)

这个问题是众所周知的。该解决方案在GitHub中进行了描述:issue 1

解决方案对我有用。