1.继承MKAnnotationView.
HYAnnotationView.h文件:
+ (instancetype)annotationViewWithMapView:(MKMapView *)mapView;
HYAnnotationView.m文件:
#import "HYAnnotation.h"//导入大头针model的头文件
@interface HYAnnotationView()
@property (nonatomic, weak) UIImageView *iconView;
@end
@implementation HYAnnotationView
+ (instancetype)annotationViewWithMapView:(MKMapView *)mapView
{
static NSString *ID = @"anno";
HYAnnotationView *annotationView = (HYAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annotationView == nil) {
// 传入循环利用标识来创建大头针控件
annotationView = [[HMAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
}
return annotationView;
}
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
// 显示标题和子标题 当自定义大头针的时候,如果需要显示标题和子标题,必须写这句话
self.canShowCallout = YES;
在这里面开始自定义大头针,可以随便创建个view
[self setsubview];
}
return self;
}
//自定义大头针
-(void)setsubview{
self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);
UILabel *nameLab =[[UILabel alloc]init];
nameLab.width = 40;
nameLab.height = 30;
nameLab.y = 0;
nameLab.x = (self.width - nameLab.width) * 0.5;
nameLab.font =IWTextFont16;
nameLab.backgroundColor =IWTextColorRed;
nameLab.textColor =[UIColor whiteColor];
nameLab.layer.cornerRadius = 8.0;
nameLab.layer.borderWidth = 1.0;
nameLab.layer.borderColor =[UIColor clearColor].CGColor;
nameLab.clipsToBounds = TRUE;//去除边界
nameLab.textAlignment =NSTextAlignmentCenter;
[self addSubview:nameLab];
self.nameLab =nameLab;
}
//设置大头针上的属性
- (void)setAnnotation:(HMAnnotation *)annotation
{
[super setAnnotation:annotation];
}
//点击大头针的时候,在大头针上加一个自定义的View(如果想放tableview可以把tableview放在这个view里面)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (self.selected == selected) return;
if (selected)
{
if (self.calloutView == nil) //self.calloutView是自定义view的属性
{
self.calloutView = [[ZLCustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
self.calloutView.centerX =self.width/2.f+ + self.calloutOffset.x;
// self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
// -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y) ;
}
[self addSubview:self.calloutView];
}
else
{
[self.calloutView removeFromSuperview];
}
[super setSelected:selected animated:animated];
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL inside = [super pointInside:point withEvent:event];
if (!inside && self.selected)
{
inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
}
return inside;
}
在用的时候:
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(HYAnnotation *)annotation
{
// 返回nil就会按照系统的默认做法
if (![annotation isKindOfClass:[HYAnnotation class]]) return nil;
// 1.获得大头针控件
HYAnnotationView *annoView = [HYAnnotationView annotationViewWithMapView:mapView];
// 2.传递模型
annoView.annotation = annotation;
return annoView;
}