MKAnnotation大头针学习一

本文介绍如何在iOS应用中使用系统默认的大头针MKAnnotation,并提供了自定义大头针的具体实现方法,包括设置颜色、动画效果及显示标题等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MKAnnotation

系统默认的大头针属性

//位置
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@optional
//主标题和子标题
@property (nonatomic, readonly, copy, nullable) NSString *title;
@property (nonatomic, readonly, copy, nullable) NSString *subtitle;

自定义大头针

  • 需要遵循大头针MKAnnotation协议
//在下面这个方法中实现自定义的大头针
 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{}

注意

过程类似于tableView中自定义cell

部分代码

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //取出可以循环利用的大头针
    static NSString * ID = @"anno";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    if (annotationView == nil) {
        //没有循环利用的则自己创建新的
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:ID];
        //设置大头针的颜色iOS9.0以前的方法
        //annotationView.pinColor = MKPinAnnotationColorGreen;
        //iOS9.0以后的方法
        annotationView.pinTintColor = [UIColor blackColor];
        //从天而降
        annotationView.animatesDrop = YES;
        //是否显示标题
        annotationView.canShowCallout = YES;
        //标题显示的位置
        annotationView.calloutOffset = CGPointMake(0, -10);//向上移动10的距离
        //右边显示的View
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
        //左边显示的View
        annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
        //底部显示细节View,会覆盖subtitle-----------通过这种方法可以插入图片
        annotationView.detailCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"IMG_0380" ]];
    }
    //传递模型覆盖掉之前的数据
    annotationView.annotation = annotation;
    return annotationView;
}

部分截图
这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值