【问题标题】:Not getting button action/ Single tap on custom MKannotation call out view未获得按钮操作/单击自定义 MKannotation 调用视图
【发布时间】:2014-02-24 11:31:12
【问题描述】:

我为我的地图视图创建了自定义注释和标注。当用户单击标注视图或单击添加为标注视图的子视图的按钮时,我需要导航到另一个视图。但是在这种情况下,手势识别器和添加目标都不适合我。 setSelected: 方法被调用,当在调用视图中点击时视图被隐藏。

 @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];
}

【问题讨论】:

  • 你在设置 calloutView.frame 和 infoButton.frame 吗?
  • - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
  • @Anna 是的。我正在设置信息按钮的框架并将标注视图添加为 [self addSubview:calloutView];

标签: ios objective-c mkmapview mkannotation mkannotationview


【解决方案1】:
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if ([hitView isKindOfClass:[UIButton class]]) {

    }
}

【讨论】:

  • 没有进入条件。还检查了条件 if ([hitView isKindOfClass:[calloutView class]]) ... 但是当用户单击注释引脚时会调用它。不是在调用视图单击时!!
  • NSLog(@"%@",hitView);
  • 单击注释时,会显示调出视图并记录视图名称。但是,当点击调用视图时,它会完美地关闭,但日志会打印(null)
【解决方案2】:

我终于得到了答案。它在这里

Followed this tutorial。真的很棒的解决方案。

https://github.com/nfarina/calloutview

编码愉快!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2011-07-23
    • 1970-01-01
    相关资源
    最近更新 更多