将UIButton添加到MKAnnotationView [英] Adding UIButton to MKAnnotationView

查看:86
本文介绍了将UIButton添加到MKAnnotationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MKAnnotationView的自定义类,我试图在其中以循环方式向注解中添加8个按钮.我已经添加了按钮.它们在视图中的位置非常好,但操作按钮没有被调用.为什么未调用该操作以及解决方案.

I've a custom class of MKAnnotationView in which I'm trying to add 8 button to the annotation in a circular fashion.I've added the buttons.Their postion in the view is perfectly fine but the action of the buttons doesn't get called.Why the action is not getting called and what is the solution.

-(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
{

}

推荐答案

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

I couldn't find any solution for this.So,I think this is not possible with this method.If still some one wants to implement this functionality then one has to use draw them on an UIiew whose user interaction is disabled.Add this UIView on the MKMapView. Use the method didSelectAnnotationView with [mapView deselectAnnotation:view.annotation animated:YES];if needed.

这篇关于将UIButton添加到MKAnnotationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆