1

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
{

}
4

3 に答える 3

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

これがお役に立てば幸いです...

于 2013-01-02T11:23:13.753 に答える
0

これは、circularView が注釈フレームの外側にあるためです。self.frame をcircularViewのサイズに拡張すると機能します。ただし、ピンなどによっては見栄えが悪い場合があります。

于 2013-11-04T13:15:44.727 に答える
0

私はこれに対する解決策を見つけることができませんでした.だから、これはこのメソッドでは不可能だと思います.それでも誰かがこの機能を実装したい場合は、ユーザーインタラクションが無効になっているUIiewでそれらを描画する必要があります.このUIViewを追加してくださいMKMapViewで。必要に応じてメソッドdidSelectAnnotationViewを使用してください。[mapView deselectAnnotation:view.annotation animated:YES];

于 2013-01-03T04:03:09.550 に答える