• iOS 地图中自定义大头针


    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;

    }

  • 相关阅读:
    luogu P4544 [USACO10NOV]Buying Feed G 斜率优化dp 双层?
    luogu P3594 [POI2015]WIL-Wilcze doły 单调队列dp+双指针
    luogu P2384 最短路 spfa+数学?
    luogu P2071 座位安排 二分图最大匹配 双重的
    luogu P1841 [JSOI2007]重要的城市 dp+Floyd
    luogu P2034 选择数字 单调队列优化dp 脑残行为,导致wa了很多遍
    【最短路-判断正权环 Floyd】Currency Exchange POJ
    【最短路-判断正权环 Bellman-Ford】Arbitrage POJ
    【最短路/矩阵+最小环】0 or 1 HDU
    【最短路+区间枚举】昂贵的聘礼 POJ
  • 原文地址:https://www.cnblogs.com/hongyan1314/p/5802533.html
Copyright © 2020-2023  润新知