Ios 按钮事件不在地图上的CustomView内部调用?

Ios 按钮事件不在地图上的CustomView内部调用?,ios,swift,dictionary,Ios,Swift,Dictionary,我正在我的应用程序中集成地图注释。我正在地图上添加注释,如下所示。所有注释都已成功添加到地图上 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if (annotation is MKUserLocation) { return nil } let reuseId

我正在我的应用程序中集成地图注释。我正在地图上添加注释,如下所示。所有注释都已成功添加到地图上

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if (annotation is MKUserLocation) {
            return nil
        }

        let reuseId = "CustomAnnotation"
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        annotationView.image = UIImage(named:"MapTemp")
        annotationView.canShowCallout = false

        return annotationView
    }
我在点击地图注释时显示一个自定义视图。单击注释时,将调用其委托,其中自定义视图将添加到地图上,如下代码所示-

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
    {
        if (self.viewCustom != nil)
        {
            self.viewCustom.removeFromSuperview()
        }

        let nib:Array = NSBundle.mainBundle().loadNibNamed("View123", owner: self, options: nil)
        self.viewCustom = nib[0] as? UIView

        println(self.viewCustom.viewWithTag(100))


        if let buttonFB = self.viewCustom.viewWithTag(100) as? UIButton {

            buttonFB.addTarget(self, action:"clickFacebook:", forControlEvents: UIControlEvents.TouchUpInside)
        }

        self.addBounceAnimationToView(viewCustom)

        var hitPoint : CGPoint = view.convertPoint(CGPointZero, toView: viewCustom)

        self.viewCustom!.frame = CGRectMake(-100,-200, 250, 200)

        view.addSubview(self.viewCustom!)
        self.view.bringSubviewToFront(self.viewCustom)

    }
在自定义视图中,我有一个指定了标记值和事件clickFacebook的buttonFB按钮

但此按钮的单击事件不会触发,并且只有Mapview的委托注释视图始终在调用方法的下方被调用

func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
    {
      //
    }
我想触发按钮的事件。我该怎么做呢

暗光线图像是MapView的注释,黄色部分是根据图像设置的按钮

请帮帮我。

在DID中选择AnnotationView:您将self.view置于前面的方法,因此self.view将位于包含地图的顶部,因此事件将在self视图上运行。您的self.viewCustom位于self.view下方,当您触摸self.view时,手柄会触摸

您需要将您的self.viewCustom置于self.view的顶部,这样它才能进行触摸

你需要改变陈述-

self.viewCustom.bringSubviewTofront self.view


self.view.bringsubview到front self.viewCustom

您的customview的类是什么?是否将userInteractionEnabled设置为YES?

完成添加的注释后,使用此方法

并确保您有自定义AnnotationView类来覆盖几个方法

下面是注释委托方法

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{

    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    else if ([annotation isKindOfClass:[CustomAnnotation class]]) // use whatever annotation class you used when creating the annotation
    {
        CustomAnnotation *anno = (CustomAnnotation *)annotation;
        static NSString *defaultPinID = @"customIdentifier";
        MyAnnotationView *pinView = (MyAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

        if (pinView == nil)
            pinView = [[MyAnnotationView alloc] initWithAnnotation:anno reuseIdentifier:defaultPinID];
        pinView.canShowCallout = NO;
        pinView.draggable = NO;
        pinView.annotation = anno;

        NSURL *url = //Custom URL and Used SDWebImage to load custom url on UIImageView
        [pinView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default-icon.png"]];


        return pinView;

    }
    return nil;
}
MyAnnotationView.m

#import "MyAnnotationView.h"

@implementation MyAnnotationView

- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView != nil) {
        [[self superview] bringSubviewToFront:self];
    }
    return hitView;
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    CGRect newRect = self.bounds;
    BOOL isInside = CGRectContainsPoint(newRect, point);
    if (isInside == NO) {

        for (UIView *view in self.subviews) {

            isInside = CGRectContainsPoint(view.frame, point);
            if (isInside == YES) {
                break;
            }

        }
    }
    return  isInside;
}

@end
下面是注释视图的选择和取消选择方法

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        CustomAnnotation *anno = (CustomAnnotation *)view.annotation;
        NSDictionary *dict = anno.annoDict;

        if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
            return;
        }

//This is custom UIView class only with subviews as UILabel, UIImageView, UIButton

        CustomCallOut *callOutView = [[CustomCallOut alloc] initWithFrame:CGRectMake(0.0, 0.0, 240.0, 80.0)];
        [callOutView setupControls];
        callOutView.lblName.text = @"title";


        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
        newButton.frame = callOutView.btnCallOut.frame;
        [newButton addTarget:self action:@selector(btnnextClick:) forControlEvents:UIControlEventTouchUpInside];
        [callOutView addSubview:newButton];


        [callOutView setCenter:CGPointMake(view.bounds.size.width / 2, -callOutView.bounds.size.height*0.52)];

        callOutView.userInteractionEnabled = true;
        [view addSubview:callOutView];

        [mapview setCenterCoordinate:anno.coordinate];
    }
取消选择方法

这是按钮点击事件

-(void)btnnextClick:(UIButton *)sender {

//Do your stuff

}

为什么你不去地图的callout附件视图,在那里你会得到附件按钮,点击它,一些操作会被执行。但我想自定义所有视图,在视图中添加了大约5个按钮。如果是这样的话,你的黄色视图应该是一个注释,它会在点击图像时弹出。不是,调用注释视图时出现相同的问题。非按钮事件调用。@KiritModi是否更改了上述语句?是的,按上述更改并重试,但问题相同。@KiritModi是否已将viewCustom上的按钮添加为viewCustom.addSubviewself.buttonFB!在XIB视图中添加按钮,并将其标记设置为100。
    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        CustomAnnotation *anno = (CustomAnnotation *)view.annotation;
        NSDictionary *dict = anno.annoDict;

        if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
            return;
        }

//This is custom UIView class only with subviews as UILabel, UIImageView, UIButton

        CustomCallOut *callOutView = [[CustomCallOut alloc] initWithFrame:CGRectMake(0.0, 0.0, 240.0, 80.0)];
        [callOutView setupControls];
        callOutView.lblName.text = @"title";


        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
        newButton.frame = callOutView.btnCallOut.frame;
        [newButton addTarget:self action:@selector(btnnextClick:) forControlEvents:UIControlEventTouchUpInside];
        [callOutView addSubview:newButton];


        [callOutView setCenter:CGPointMake(view.bounds.size.width / 2, -callOutView.bounds.size.height*0.52)];

        callOutView.userInteractionEnabled = true;
        [view addSubview:callOutView];

        [mapview setCenterCoordinate:anno.coordinate];
    }
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(nonnull MKAnnotationView *)view
{
    if ([view isKindOfClass:[MyAnnotationView class]]) {
        for (CustomCallOut *subview in view.subviews)
        {
            if ([subview isKindOfClass:[CustomCallOut class]]) {
                [subview removeFromSuperview];
            }
        }
    }
}
-(void)btnnextClick:(UIButton *)sender {

//Do your stuff

}