按钮事件是否不在MAP上的CustomView内部调用?

时间:2015-05-25 09:29:38

标签: ios swift dictionary

我正在我的应用中集成地图注释。我在地图上添加注释如下。所有注释都在MAP上成功添加。

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
    }

我在点击地图注释时显示了一个customView。单击注释时,将调用其委托,在地图上添加自定义视图,如下面的代码所示 -

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)

    }

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

@IBAction func clickFacebook(sender: AnyObject)
 {
     //Face
 }

但是点击此按钮的事件不会触发,并且始终只调用Mapview的委托didDeselectAnnotationView(在方法下面调用)。

func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
    {
      //
    }

我希望触发按钮的事件。我怎么能这样做。

enter image description here

darkgray图像是MapView的注释,黄色部分是根据图像的按钮。

请帮帮我。

3 个答案:

答案 0 :(得分:0)

didSelectAnnotationView:内部self.view方法中,self.view位于前面,因此self view将位于顶部,其中包含地图,因此事件将在self.viewCustom上运行。您的self.view低于self.view,当您触摸self.viewCustom处理触摸时。

您需要将self.view置于self.viewCustom.bringSubviewToFront(self.view)的顶部,然后才可以接触。

您需要更改声明 -

self.view.bringSubviewToFront(self.viewCustom)

{% static "css/style.css" %}

答案 1 :(得分:0)

您的自定义视图的类是什么?您是否将YES设置为onPrepareOptionsMenu()

答案 2 :(得分:0)

完成添加注释后,请转到此方法。

并确保您使用自定义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 ** ***这是完成此任务的关键代码。*

MyAnnotationView.h

#import <MapKit/MapKit.h>

@interface MyAnnotationView : MKAnnotationView

@end

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)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

}