RMMapOverlayView.m
4.65 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
//
// RMMapOverlayView.m
// MapView
//
// Created by Thomas Rasch on 25.08.11.
// Copyright (c) 2011 Alpstein. All rights reserved.
//
#import "RMMapOverlayView.h"
#import "RMMarker.h"
@interface RMMapOverlayView ()
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer;
- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer;
@end
@implementation RMMapOverlayView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return nil;
self.layer.masksToBounds = YES;
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];
[self addGestureRecognizer:singleTapRecognizer];
[self addGestureRecognizer:doubleTapRecognizer];
return self;
}
- (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];
}
#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 YES;
}
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer
{
CALayer *hit = [self.layer hitTest:[recognizer locationInView:self]];
if (hit != nil)
{
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 != nil)
{
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]];
}
}
}
}
@end