iOS学习iOSiOS-移动架构师

iOS 大头针基本使用

2015-11-12  本文已影响1736人  iOS_成才录

一、简介

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

二、使用

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
        {
            if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return nil;
            }
            // 如果此方法返回nil, 就会使用系统自带的大头针视图
            // 模拟下,返回nil,系统的解决方案
            static NSString *pinId = @"pinID";
            MKPinAnnotationView *pinView = ( MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinId];
            if (pinView == nil) {
                pinView  = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinId];
            }
            pinView.annotation = annotation;
            // 是否显示标注
            pinView.canShowCallout = YES;
            // 设置大头针颜色
            pinView.pinColor = MKPinAnnotationColorPurple;
            // 设置大头针是否有下落动画
            pinView.animatesDrop = YES;
            return pinView;
        }
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
        {
            if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return nil;
            }
            /**  自定义大头针-------*/
            static NSString *pinId = @"pinID";
            MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinId];
            if (annoView == nil) {
                annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinId];
            }
            annoView.annotation = annotation;
            annoView.image = [UIImage imageNamed:@"category_5"];
            annoView.canShowCallout = YES;
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"huba.jpeg"]];
            imageView.bounds = CGRectMake(0, 0, 44, 44);
            annoView.leftCalloutAccessoryView = imageView;
            imageView.userInteractionEnabled  = YES;
            UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"eason.jpg"]];
            imageView2.bounds = CGRectMake(0, 0, 44, 44);
            annoView.rightCalloutAccessoryView = imageView2;
            annoView.detailCalloutAccessoryView = [UISwitch new];
            annoView.draggable = YES;
            return annoView;
        }
// 点击标注
        - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
        {
            NSLog(@"点击标注");
        }
        // 选中大头针
        - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
        {
            NSLog(@"选中大头针");
        }
        // 取消选中大头针
        -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
        {
            NSLog(@"取消选中大头针");
        }
上一篇 下一篇

猜你喜欢

热点阅读