RMMapOverlayView.m
7.32 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// RMMapOverlayView.m
// MapView
//
// Created by Thomas Rasch on 25.08.11.
// Copyright (c) 2011 Alpstein. All rights reserved.
//
#import "RMMapOverlayView.h"
#import "RMMarker.h"
#import "RMAnnotation.h"
#import "RMPixel.h"
@interface RMMapOverlayView ()
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer;
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer;
@end
@implementation RMMapOverlayView
@synthesize delegate;
+ + layerClass
{
return [CAScrollLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;
self.layer.masksToBounds = YES;
_trackPanGesture = NO;
_lastTranslation = CGPointZero;
_draggedAnnotation = nil;
UITapGestureRecognizer *doubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] autorelease];
doubleTapRecognizer.numberOfTapsRequired = 2;
UITapGestureRecognizer *singleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)] autorelease];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
UIPanGestureRecognizer *panGestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)] autorelease];
panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = 1;
[self addGestureRecognizer:singleTapRecognizer];
[self addGestureRecognizer:doubleTapRecognizer];
[self addGestureRecognizer:panGestureRecognizer];
return self;
}
- (void)dealloc
{
[_draggedAnnotation release]; _draggedAnnotation = nil;
[super dealloc];
}
- (unsigned)sublayersCount
{
return [self.layer.sublayers count];
}
- (void)addSublayer:(CALayer *)aLayer
{
[self.layer addSublayer:aLayer];
}
- (void)insertSublayer:(CALayer *)aLayer atIndex:(unsigned)index
{
[self.layer insertSublayer:aLayer atIndex:index];
}
- (void)insertSublayer:(CALayer *)aLayer below:(CALayer *)sublayer
{
[self.layer insertSublayer:aLayer below:sublayer];
}
- (void)insertSublayer:(CALayer *)aLayer above:(CALayer *)sublayer
{
[self.layer insertSublayer:aLayer above:sublayer];
}
- (void)moveLayersBy:(CGPoint)delta
{
[self.layer scrollPoint:CGPointMake(-delta.x, -delta.y)];
}
#pragma mark -
#pragma mark Event handling
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if ([[event touchesForView:self] count] > 1)
return NO;
CALayer *hit = [self.layer hitTest:point];
if (!hit || ![hit isKindOfClass:[RMMarker class]]) {
return NO;
}
return ((RMMarker *)hit).annotation.enabled;
}
- (RMAnnotation *)findAnnotationInLayer:(CALayer *)layer
{
if ([layer respondsToSelector:@selector(annotation)])
return [((RMMarker *)layer) annotation];
CALayer *superlayer = [layer superlayer];
if (superlayer != nil && [superlayer respondsToSelector:@selector(annotation)])
return [((RMMarker *)superlayer) annotation];
else if ([superlayer superlayer] != nil && [[superlayer superlayer] respondsToSelector:@selector(annotation)])
return [((RMMarker *)[superlayer superlayer]) annotation];
return nil;
}
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer
{
CALayer *hit = [self.layer hitTest:[recognizer locationInView:self]];
if (!hit) return;
CALayer *superlayer = [hit superlayer];
// See if tap was on a marker or marker label and send delegate protocol method
if ([hit isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:tapOnAnnotation:atPoint:)]) {
[delegate mapOverlayView:self tapOnAnnotation:[((RMMarker *)hit) annotation] atPoint:[recognizer locationInView:self]];
}
} else if (superlayer != nil && [superlayer isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:tapOnLabelForAnnotation:atPoint:)]) {
[delegate mapOverlayView:self tapOnLabelForAnnotation:[((RMMarker *)superlayer) annotation] atPoint:[recognizer locationInView:self]];
}
} else if ([superlayer superlayer] != nil && [[superlayer superlayer] isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:tapOnLabelForAnnotation:atPoint:)]) {
[delegate mapOverlayView:self tapOnLabelForAnnotation:[((RMMarker *)[superlayer superlayer]) annotation] atPoint:[recognizer locationInView:self]];
}
}
}
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer
{
CALayer *hit = [self.layer hitTest:[recognizer locationInView:self]];
if (!hit) return;
CALayer *superlayer = [hit superlayer];
// See if tap was on a marker or marker label and send delegate protocol method
if ([hit isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:doubleTapOnAnnotation:atPoint:)]) {
[delegate mapOverlayView:self doubleTapOnAnnotation:[((RMMarker *)hit) annotation] atPoint:[recognizer locationInView:self]];
}
} else if (superlayer != nil && [superlayer isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:doubleTapOnLabelForAnnotation:atPoint:)]) {
[delegate mapOverlayView:self doubleTapOnLabelForAnnotation:[((RMMarker *)superlayer) annotation] atPoint:[recognizer locationInView:self]];
}
} else if ([superlayer superlayer] != nil && [[superlayer superlayer] isKindOfClass:[RMMarker class]]) {
if ([delegate respondsToSelector:@selector(mapOverlayView:doubleTapOnLabelForAnnotation:atPoint:)]) {
[delegate mapOverlayView:self doubleTapOnLabelForAnnotation:[((RMMarker *)[superlayer superlayer]) annotation] atPoint:[recognizer locationInView:self]];
}
}
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
CALayer *hit = [self.layer hitTest:[recognizer locationInView:self]];
if (!hit) return;
if ([hit respondsToSelector:@selector(enableDragging)] && ![(RMMarker *)hit enableDragging]) return;
_lastTranslation = CGPointZero;
[_draggedAnnotation release];
_draggedAnnotation = [[self findAnnotationInLayer:hit] retain];
if ([delegate respondsToSelector:@selector(mapOverlayView:shouldDragAnnotation:)])
_trackPanGesture = [delegate mapOverlayView:self shouldDragAnnotation:_draggedAnnotation];
else
_trackPanGesture = NO;
}
if (!_trackPanGesture) return;
if (recognizer.state == UIGestureRecognizerStateChanged && [delegate respondsToSelector:@selector(mapOverlayView:didDragAnnotation:withDelta:)]) {
CGPoint translation = [recognizer translationInView:self];
CGPoint delta = CGPointMake(_lastTranslation.x - translation.x, _lastTranslation.y - translation.y);
_lastTranslation = translation;
[delegate mapOverlayView:self didDragAnnotation:_draggedAnnotation withDelta:delta];
} else if (recognizer.state == UIGestureRecognizerStateEnded && [delegate respondsToSelector:@selector(mapOverlayView:didEndDragAnnotation:)]) {
[delegate mapOverlayView:self didEndDragAnnotation:_draggedAnnotation];
_trackPanGesture = NO;
[_draggedAnnotation release]; _draggedAnnotation = nil;
}
}
@end