/
NAPinAnnotationMapView.m
107 lines (82 loc) · 2.84 KB
/
NAPinAnnotationMapView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// NAPinAnnotationMapView.m
// NAMapKit
//
// Created by Daniel Doubrovkine on 3/7/14.
// Copyright (c) 2010-14 neilang.com. All rights reserved.
//
#import "NAPinAnnotationMapView.h"
const CGFloat NAMapViewAnnotationCalloutAnimationDuration = 0.1f;
@interface NAPinAnnotationMapView()
@property (nonatomic, strong) NAPinAnnotationCallOutView *calloutView;
- (IBAction)showCallOut:(id)sender;
- (void)hideCallOut;
@end
@implementation NAPinAnnotationMapView
- (void)setupMap
{
[super setupMap];
_calloutView = [[NAPinAnnotationCallOutView alloc] initOnMapView:self];
[self addSubview:self.calloutView];
}
- (void)addAnnotation:(NAAnnotation *)annotation animated:(BOOL)animate
{
[super addAnnotation:annotation animated:animate];
if ([annotation.view isKindOfClass:NAPinAnnotationView.class]) {
NAPinAnnotationView *annotationView = (NAPinAnnotationView *) annotation.view;
[annotationView addTarget:self action:@selector(showCallOut:) forControlEvents:UIControlEventTouchDown];
}
[self bringSubviewToFront:self.calloutView];
}
- (void)selectAnnotation:(NAAnnotation *)annotation animated:(BOOL)animate
{
[self hideCallOut];
if([annotation isKindOfClass:NAPinAnnotation.class]) {
[self showCalloutForAnnotation:(NAPinAnnotation *)annotation animated:animate];
}
}
- (void)removeAnnotation:(NAAnnotation *)annotation
{
[self hideCallOut];
[super removeAnnotation:annotation];
}
- (IBAction)showCallOut:(id)sender
{
if([sender isKindOfClass:[NAPinAnnotationView class]]) {
NAPinAnnotationView *annontationView = (NAPinAnnotationView *)sender;
if ([self.mapViewDelegate respondsToSelector:@selector(mapView:tappedOnAnnotation:)]) {
[self.mapViewDelegate mapView:self tappedOnAnnotation:annontationView.annotation];
}
[self showCalloutForAnnotation:annontationView.annotation animated:YES];
}
}
- (void)showCalloutForAnnotation:(NAPinAnnotation *)annotation animated:(BOOL)animated
{
[self hideCallOut];
self.calloutView.annotation = annotation;
[self centerOnPoint:annotation.point animated:animated];
CGFloat animationDuration = animated ? NAMapViewAnnotationCalloutAnimationDuration : 0.0f;
self.calloutView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4f, 0.4f);
self.calloutView.hidden = NO;
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:animationDuration animations:^{
weakSelf.calloutView.transform = CGAffineTransformIdentity;
}];
}
- (void)hideCallOut
{
self.calloutView.hidden = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
[self hideCallOut];
}
[super touchesEnded:touches withEvent:event];
}
- (void)updatePositions
{
[self.calloutView updatePosition];
[super updatePositions];
}
@end