简体   繁体   English

如何在自定义MKAnnotationView中检测触摸? IOS 7

[英]How can I detect touch in custom MKAnnotationView? ios7

This not works 这行不通

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

Tried add this 尝试添加此

  if ([annotation isKindOfClass:[User class]])
     {
         NSString * annotationIdentifier = @"UserAnnotationIdentifier";
         CustomAnnotationView * customAnnotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
         if (!customAnnotationView)
         {
             customAnnotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
             UITapGestureRecognizer *tapGesture =
             [[UITapGestureRecognizer alloc] initWithTarget:self
                                                     action:@selector(calloutTapped:)];
             [customAnnotationView addGestureRecognizer:tapGesture];

and then 接着

    -(void) calloutTapped:(id) sender {
       id<MKAnnotation> annotation = ((MKAnnotationView*)sender.view).annotation;

ERROR: property view not found on object of type __strong id 错误:在类型为__strong id的对象上找不到属性视图

This works, but not always 这可行,但并非总是如此

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
 {
NSLog(@"didSelectAnnotationView\n");

 }

 - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
 {
NSLog(@"didDeselectAnnotationView\n");
 }

Where I bad? 我哪里不好?

Please try this.And make sure you set the MKMapViewDelegate to that class. 请尝试此操作。并确保将MKMapViewDelegate设置为该类。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        if([view isKindOfClass:[myCustomAnnotaionClass class]])
        {
            //your programming logic here
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM