简体   繁体   中英

Not getting button action/ Single tap on custom MKannotation call out view

I have created a custom annotation and a call out for my map view. I need to navigate to another view when the user clicks on call out view or he clicks to the button that added as sub view to the callout view. But both gesture recognizer and add target is not working for me in this case. The setSelected: method was invoked and the view get hidden when tap occurs in call out view.

 @interface VBPunchCardAnnotation : MKAnnotationView{

    UIView *calloutView;
  }

- (id)initWithAnnotation:(id )annotation reuseIdentifier:(NSString *)reuseIdentifier deal:(id)punchdeal
{
  self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  calloutView = [[UIView alloc] init];
  calloutView.hidden = YES;

  infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

  [calloutView addSubview:infoButton];

  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationTapped:)];
  singleTap.numberOfTapsRequired = 1;
  singleTap.delegate = self;
  [calloutView addGestureRecognizer:singleTap];

  [infoButton addTarget:self action:@selector(annotationTapped:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:calloutView];

   return self;

}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
 // show/hide callout and swap pin image
  calloutView.hidden = !selected;
  self.image = !selected ? normalPin : selectedPin;
  // dispatch an event to alert app a pin has been selected

  if(selected) [[NSNotificationCenter defaultCenter] postNotificationName:@"punchCardAnnotation" object:self];
}

-(void)annotationTapped:(id)sender{
   [self.delegate punchCardAnnotationClickedForDeal:self.punchDeal];
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if ([hitView isKindOfClass:[UIButton class]]) {

    }
}

Finally I got the answer. It;s here

Followed this tutorial . Really great solution.

https://github.com/nfarina/calloutview

Happy coding!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM