| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- //
- // AMAnnotationView.m
- // Apex Mobile
- //
- // Created by Jack on 2018/5/3.
- // Copyright © 2018年 United Software Applications, Inc. All rights reserved.
- //
- #import "AMAnnotationView.h"
- #import "AMMapAnnotaion.h"
- @interface AMAnnotationView ()
- @property (strong,nonatomic) UIView *calloutView;
- @property (strong,nonatomic) AMMapAnnotaion *myAnnotation;
- @property (nonatomic,strong) UILabel *titleLabel;
- @property (nonatomic,strong) UILabel *detailLabel;
- @property (nonatomic,assign) BOOL tapedCallout;
- @end
- @implementation AMAnnotationView
- - (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
-
- if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
-
- self.opaque = NO;
- self.canShowCallout = NO;
- self.myAnnotation = (AMMapAnnotaion *)annotation;
- self.calloutView = [[UIView alloc] init];
- self.calloutView.backgroundColor = [UIColor whiteColor];
- }
- return self;
- }
- - (UILabel *)titleLabel {
- if (!_titleLabel) {
- _titleLabel = [[UILabel alloc] init];
- _titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
- _titleLabel.textColor = [UIColor blackColor];
- _titleLabel.textAlignment = NSTextAlignmentLeft;
- _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- _titleLabel.numberOfLines = 1;
- }
- return _titleLabel;
- }
- - (UILabel *)detailLabel {
- if (!_detailLabel) {
- _detailLabel = [[UILabel alloc] init];
- _detailLabel.font = [UIFont systemFontOfSize:14.0f];
- _detailLabel.textColor = [UIColor blackColor];
- _detailLabel.textAlignment = NSTextAlignmentLeft;
- _detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
- _detailLabel.numberOfLines = 0;
- }
- return _detailLabel;
- }
- - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
-
- UIView *hitView = [super hitTest:point withEvent:event];
- self.tapedCallout = NO;
- if (self.isSelected) {
- if (hitView) {
- // self.canDeselected = NO;
- return self;
- }
- if (CGRectContainsPoint(self.calloutView.frame, point)) {
- // hitView = self.calloutView;
- self.tapedCallout = YES;
- return self.calloutView;
- }
- }
- return hitView;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- if (self.isSelected && self.tapedCallout) {
- if (self.delegate && [self.delegate respondsToSelector:@selector(mapView:didTapAnnotaionCallout:)]) {
- [self.delegate mapView:[self findMap:self] didTapAnnotaionCallout:self.myAnnotation];
- }
- }
- [super setSelected:selected animated:YES];
- // Get the custom callout view.
- UIView *calloutView = self.calloutView;
- if (selected) {
- NSString *title = self.myAnnotation.title;
- NSString *detail = self.myAnnotation.subtitle;
- CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.8f;
- CGFloat w0 = maxWidth;
- CGRect frame = CGRectZero;
- self.titleLabel.text = title;
- [self.titleLabel sizeToFit];
- frame = self.titleLabel.bounds;
- frame.origin = CGPointMake(5, 5);
- w0 = CGRectGetWidth(frame);
- if (w0 > maxWidth) {
- w0 = maxWidth;
- frame.size.width = w0;
- }
- self.titleLabel.frame = frame;
- [self.calloutView addSubview:self.titleLabel];
- CGFloat w1 = maxWidth;
- self.detailLabel.text = detail;
- [self.detailLabel sizeToFit];
- frame.origin.y = CGRectGetMaxY(frame) + 5;
- frame.size = self.detailLabel.bounds.size;
- w1 = CGRectGetWidth(frame);
- if (w1 > maxWidth) {
- w1 = maxWidth;
- frame.size.width = w1;
- }
- self.detailLabel.frame = frame;
- [self.calloutView addSubview:self.detailLabel];
- CGFloat h = CGRectGetMaxY(frame) + 5 + 10;
- MKMapView *map = [self findMap:self];
- // frame = self.frame;
- //
- // // 将左右边距小于30的移动30
- // if (frame.origin.x < 30) {
- //
- // if (map != nil) {
- // MKCoordinateRegion region = map.region;
- // CGPoint center = map.center;
- // center.x -= 30;
- // region.center = [map convertPoint:center toCoordinateFromView:map];
- // [map setRegion:region animated:NO];
- // }
- // }
- // if (CGRectGetMaxX(frame) > CGRectGetWidth(map.bounds) - 30) {
- // if (map != nil) {
- // MKCoordinateRegion region = map.region;
- // CGPoint center = map.center;
- // center.x += 30;
- // region.center = [map convertPoint:center toCoordinateFromView:map];
- // [map setRegion:region animated:NO];
- // }
- //
- // }
- //
- // if (CGRectGetMinY(frame) < h) {
- // if (map != nil) {
- //
- // MKCoordinateRegion region = map.region;
- // CGPoint center = map.center;
- // center.y += h + CGRectGetHeight(frame);
- // region.center = [map convertPoint:center toCoordinateFromView:map];
- // [map setRegion:region animated:NO];
- //
- // }
- // }
- CGPoint center = self.center;
- MKCoordinateRegion region = map.region;
- region.center = [map convertPoint:center toCoordinateFromView:map];
- [map setRegion:region animated:NO];
- frame = self.frame;
- CGFloat w = MAX(w0, w1) + 5 * 2;
- self.calloutView.frame = CGRectMake((CGRectGetWidth(frame) - w) * 0.5, -h, w, h);
- UIBezierPath *path = [self pathForCalloutWithWidth:w Height:h Anchor:CGPointMake(w * 0.5, h)];
- CAShapeLayer *mask = [CAShapeLayer layer];
- mask.fillColor = [UIColor blackColor].CGColor;
- mask.shadowColor = [UIColor blackColor].CGColor;
- mask.shadowOffset = CGSizeMake(2, 2);
- mask.path = path.CGPath;
- self.calloutView.layer.mask = mask;
- [self addSubview:calloutView];
- } else {
- [calloutView removeFromSuperview];
- }
- }
- - (MKMapView *)findMap:(UIView *)view {
- if (view == nil) {
- return nil;
- }
- if (view.superview == nil) {
- if ([view isKindOfClass:[MKMapView class]]) {
- return (MKMapView *)view;
- } else {
- return nil;
- }
- }
- if (view.superview && [view.superview isKindOfClass:[MKMapView class]]) {
- return (MKMapView *)view.superview;
- } else {
- return [self findMap:view.superview];
- }
- }
- - (UIBezierPath *)pathForCalloutWithWidth:(CGFloat)w Height:(CGFloat)h Anchor:(CGPoint)anchor {
-
- UIBezierPath *path = [UIBezierPath bezierPath];
- [path moveToPoint:CGPointMake(w * 0.5, 0)];
- [path addLineToPoint:CGPointMake(w - 5, 0)];
- [path addQuadCurveToPoint:CGPointMake(w, 5) controlPoint:CGPointMake(w, 0)];
- [path addLineToPoint:CGPointMake(w, h - 15)];
- [path addQuadCurveToPoint:CGPointMake(w - 5, h - 10) controlPoint:CGPointMake(w, h - 10)];
-
- // [path addLineToPoint:CGPointMake(anchor.x + 10, h - 10)];
- // [path addQuadCurveToPoint:anchor controlPoint:CGPointMake(anchor.x + 5, h - 10)];
- // [path addQuadCurveToPoint:CGPointMake(anchor.x - 10, h - 10) controlPoint:CGPointMake(anchor.x - 5, h - 10)];
-
- [path addLineToPoint:CGPointMake(anchor.x + 6, h - 10)];
- [path addLineToPoint:anchor];
- [path addLineToPoint:CGPointMake(anchor.x - 6, h - 10)];
-
- [path addLineToPoint:CGPointMake(5, h - 10)];
- [path addQuadCurveToPoint:CGPointMake(0, h - 15) controlPoint:CGPointMake(0, h - 10)];
- [path addLineToPoint:CGPointMake(0, 5)];
- [path addQuadCurveToPoint:CGPointMake(5, 0) controlPoint:CGPointMake(0, 0)];
- [path addLineToPoint:CGPointMake(w * 0.5, 0)];
- return path;
- }
- @end
|