MKAnnotationView子视图 [英] MKAnnotationView Subviews

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

问题描述

当前,我的项目在实现具有多个自定义UIImageViews的自定义MKAnnotationView时遇到问题.因此,这些自定义UIImageViews在其顶部具有一个清除按钮,而不必添加手势识别器.

Currently, I am having an issue with my project in implementing a custom MKAnnotationView that has multiple custom UIImageViews. So these custom UIImageViews have a clear button on top of them to not have to add gesture recognizers.

如您所见,实际点击MKAnnotationView子视图并进行一些操作将是有益的.

As you can see, it would be beneficial to actually tap the MKAnnotationView subviews and have some action happen.

我为MKAnnotationView实现了一个协议,其中MKAnnotationView中的每个图像子视图向作为MKMapView所有者的控制器进行回调……这是代码...

I implemented a protocol for the MKAnnotationView where each image subview within the MKAnnotationView makes a callback to the controller that is the owner of the MKMapView... Heres the code...

PHProfileImageView *image = [[PHProfileImageView alloc] initWithFrame:CGRectMake(newX - radius / 5.0f, newY - radius / 5.0f, width, height)];
        [image setFile:[object objectForKey:kPHEventPictureKey]];
        [image.layer setCornerRadius:image.frame.size.height/2];
        [image.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        [image.layer setBorderWidth:2.0f];
        [image.layer setMasksToBounds:YES];
        [image.profileButton setTag:i];
        [image.profileButton addTarget:self action:@selector(didTapEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:image];        


- (void)didTapEvent:(UIButton *)button
{
    NSLog(@"%@", [self.pins objectAtIndex:button.tag]);
    if (self.delegate && [self.delegate respondsToSelector:@selector(didTapEvent:)]) {
        [self.delegate JSClusterAnnotationView:self didTapEvent:[self.pins objectAtIndex:button.tag]];
    }
}

因此,正如您所看到的,我已经尝试记录点击图像的结果,但是什么也没有:(.我实现该方法的方式不可行吗?我应该拥有CAShapeLayers或其他东西吗?不是真的肯定在这一点上.有人有任何想法吗?

So as you can see, I already attempt to log the result of the tapped image but nothing :(. Is the way I'm implementing this not the way to go? Am I supposed to have CAShapeLayers or something? Not really sure at this point. Anyone got any ideas?

修改

我认为我可能必须实现自定义标注视图.由于标注视图实际上在其视图中添加了按钮,并且可以响应触摸事件...虽然不能完全确定,因为标注只有在点击了注释视图后才会显示.在这种情况下,ACTUAL批注视图是中间标签

Im thinking that I might have to implement a custom callout view. Since a callout view actually adds buttons to its view and can respond to touch events... Not totally sure though because callouts are only shown once the annotation view is tapped. And in this case, the ACTUAL annotation view is the middle label

因此,我将mkannotationview的框架调整为更大的框架,并且显然所有子视图实际上不在MKAnnotationView的范围之内,因此实际上并未利用这些子视图.现在,我正在考虑此解决方案,它可能不是最佳解决方案.

So I resized the mkannotationview's frame to a much larger frame and apparently all the subviews are actually not within the MKAnnotationView's bounds, so the subviews aren't actually being tapped. Now that Im thinking about this solution, it probably wasn't the best solution.

如果有人有任何建议,而不是将子视图添加到MKAnnotationView来创建我当前拥有的视图,那太好了!

If anyone has any suggestions rather than adding subviews to a MKAnnotationView to create the view I currently have, that would be great!

推荐答案

对于带有可单击按钮的自定义AnnotationView,必须在项目中创建自定义AnnotationView子类.为此,创建一个新文件.

For the Custom AnnotationView with Clickable Buttons, you have to create custom AnnotationView SubClass in the Project. For that create a new file.

并将这两种方法添加到实现文件中.

And add these two methods to the implementation file.

- (UIView*)hitTest:(CGPoint)point withEvent:(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 rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    {
        for (UIView *view in self.subviews)
        {
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        }
    }
    return isInside;
}

然后再次转到ViewController.m文件,并以此方式修改viewDidLoad方法.

Then go to the ViewController.m file again and modify the viewDidLoad method as this.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mapKit.delegate = self;

    //Set Default location to zoom
    CLLocationCoordinate2D noLocation = CLLocationCoordinate2DMake(51.900708, -2.083160); //Create the CLLocation from user cordinates
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 50000, 50000); //Set zooming level
    MKCoordinateRegion adjustedRegion = [self.mapKit regionThatFits:viewRegion]; //add location to map
    [self.mapKit setRegion:adjustedRegion animated:YES]; // create animation zooming

    // Place Annotation Point
    MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init]; //Setting Sample location Annotation
    [annotation1 setCoordinate:CLLocationCoordinate2DMake(51.900708, -2.083160)]; //Add cordinates
    [self.mapKit addAnnotation:annotation1];
}

现在将自定义视图添加到ViewController.xib.

Now add that custom View to the ViewController.xib.

现在按如下所示创建此委托方法.

Now create this delegate method as below.

#pragma mark : MKMapKit Delegate

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    AnnotationView *pinView = nil; //create MKAnnotationView Property

    static NSString *defaultPinID = @"com.invasivecode.pin"; //Get the ID to change the pin
    pinView = (AnnotationView *)[self.mapKit dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; //Setting custom MKAnnotationView to the ID
    if ( pinView == nil )
        pinView = [[AnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID]; // init pinView with ID

    [pinView addSubview:self.customView];
    addSubview:self.customView.center = CGPointMake(self.customView.bounds.size.width*0.1f, -self.customView.bounds.size.height*0.5f);

    pinView.image = [UIImage imageNamed:@"Pin"]; //Set the image to pinView

    return pinView;
}

几个月前,我还从Stackoverflow上发布的某个人那里得到了这个答案.我根据需要将其修改为我的项目.希望这能帮到您.

I also got this answer few months ago from someone posted on Stackoverflow. I modified it to my project as I want. Hope this will do your work.

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

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