【问题标题】:Adding UIButton to MKAnnotationView将 UIButton 添加到 MKAnnotationView
【发布时间】:2013-01-02 11:12:29
【问题描述】:

我有一个MKAnnotationView 的自定义类,我正在尝试以循环方式将 8 个按钮添加到注释中。我已经添加了按钮。它们在视图中的位置非常好,但动作没有调用按钮。为什么没有调用该操作以及解决方案是什么。

-(void)setSelected:(BOOL)selected animated:(BOOL)animated`
{
    [super setSelected:selected animated:animated];
    if(selected)
    {
        UIView *circularView = [[UIView alloc]initWithFrame:CGRectMake(-55, -55, 110, 110)];
        circularView.tag = 1000;

        UIButton *btn1;
        UIButton *btn2;
        UIButton *btn3;
        UIButton *btn4;
        UIButton *btn5;
        UIButton *btn6;
        UIButton *btn7;
        btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn1];
        btn1.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(buttonTapped:)];
        tap.numberOfTapsRequired = 1;
        [btn1 addGestureRecognizer:tap];

        btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn2];

        btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn3];

        btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn4];

        btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn5];

        btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn6];

        btn7 = [UIButton buttonWithType:UIButtonTypeCustom];
        [circularView addSubview:btn7];

        [btn1 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn2 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn3 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn4 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn5 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn6 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
        [btn7 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];

        CGPoint point = CGPointMake(55, 55);
        btn1.frame = CGRectMake(point.x - 15.00, point.y - 55.00, 30, 30);
        btn2.frame = CGRectMake(point.x + 16.27, point.y - 39.94, 30, 30);
        btn3.frame = CGRectMake(point.x + 24.00, point.y - 06.10, 30, 30);
        btn4.frame = CGRectMake(point.x + 02.36, point.y + 21.04, 30, 30);
        btn5.frame = CGRectMake(point.x - 32.36, point.y + 21.04, 30, 30);
        btn6.frame = CGRectMake(point.x - 54.00, point.y - 06.10, 30, 30);
        btn7.frame = CGRectMake(point.x - 46.27, point.y - 39.94, 30, 30);
        [self addSubview:circularView];
    }
    else
    {
        [[self viewWithTag:1000] removeFromSuperview];
    }
}
-(void)buttonTapped:(UITapGestureRecognizer*)sender
{

}

【问题讨论】:

  • 如果你发布一些代码会更好......
  • 代码中没有太多内容。我只是在一个 UIView 上添加了所有按钮,并将 UIView 添加到 MKAnnotationView。
  • 那么要么你的注释没有被点击,也许有一些关于它的视图或者他们被禁用......不能告诉你任何事情当然你......
  • 点击按钮会发生什么?注释视图消失了?
  • 其实我是在注释被点击并且按钮添加成功时添加了所有按钮。我确定中间没有视图。

标签: iphone objective-c ios mkmapview mkannotation


【解决方案1】:

只需尝试像下面添加UIButton 并使用UITapGestureRecognizer 调用它的事件。请参见下面的示例...

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
        viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *annView = (MKAnnotationView *)[mapView 
            dequeueReusableAnnotationViewWithIdentifier: @"pin"];
    if (annView == nil)
    {
        annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:@"pin"] autorelease];

        annView.frame = CGRectMake(0, 0, 200, 50);

        UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        pinButton.frame = CGRectMake(0, 0, 140, 28);
        pinButton.tag = 10;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(btnPinTap:)];
        tap.numberOfTapsRequired = 1;
        [pinButton addGestureRecognizer:tap];
        [tap release];

        [annView addSubview:pinButton]; 
    }

    annView.annotation = annotation;

    UIButton *pb = (UIButton *)[annView viewWithTag:10];
    [pb setTitle:annotation.title forState:UIControlStateNormal];

    return annView;
}

- (void) btnPinTap:(UITapGestureRecognizer *)gestureRecognizer 
{
    UIButton *btn = (UIButton *) gestureRecognizer.view;
    MKAnnotationView *av = (MKAnnotationView *)[btn superview];
    id<MKAnnotation> ann = av.annotation;
    NSLog(@"btnPinTap: ann.title=%@", ann.title);
}

只需在.h 文件中添加代表..

UIGestureRecognizerDelegate

只需将此波纹管方法放入您的.m 文件中...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer
            :(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

希望对你有帮助...

【讨论】:

  • @Shubham 你试试这个??并检查其 UserInterectuion 属性是否为真
  • 是的,我已经尝试过类似的事情。但是需要启用谁的用户交互?
  • 你的按钮就像 yourButton.userInteractionEnabled = YES;
  • [circularView setUserInteractionEnabled:YES]; [自我带来SubviewToFront:圆形视图];将此行添加到您添加此圆形视图的位置
  • -(void)buttonTapped:(UITapGestureRecognizer*)sender { NSLog(@"\n\n Button Clicked Here"); } 你检查一下这个??
【解决方案2】:

我找不到任何解决方案。所以,我认为这种方法是不可能的。如果仍然有人想要实现这个功能,那么必须在禁用用户交互的 UIiew 上使用绘制它们。在 MKMapView 上添加这个 UIView。如果需要,请使用 didSelectAnnotationView[mapView deselectAnnotation:view.annotation animated:YES]; 方法。

【讨论】:

    【解决方案3】:

    这是因为circularView在注释框之外。将 self.frame 扩展为 circularView 的大小将使其工作。但是,根据其他情况、别针等,它可能看起来不太好。

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多