CCHMapViewDelegateProxy.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // CCHMapViewDelegateProxy.m
  3. // CCHMapClusterController
  4. //
  5. // Copyright (C) 2013 Claus Höfele
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "CCHMapViewDelegateProxy.h"
  26. #import "CCHMapClusterControllerDebugPolygon.h"
  27. @interface CCHMapViewDelegateProxy()
  28. @property (nonatomic) NSHashTable *delegates;
  29. @property (nonatomic, weak) NSObject<MKMapViewDelegate> *target;
  30. @property (nonatomic) MKMapView *mapView;
  31. @end
  32. @implementation CCHMapViewDelegateProxy
  33. - (instancetype)initWithMapView:(MKMapView *)mapView delegate:(NSObject<MKMapViewDelegate> *)delegate
  34. {
  35. self = [super init];
  36. if (self) {
  37. _delegates = [[NSHashTable alloc] initWithOptions:NSPointerFunctionsWeakMemory capacity:1];
  38. [_delegates addObject:delegate];
  39. _target = mapView.delegate;
  40. _mapView = mapView;
  41. [self swapDelegates];
  42. }
  43. return self;
  44. }
  45. - (void)addDelegate:(NSObject<MKMapViewDelegate> *)delegate
  46. {
  47. [self.delegates addObject:delegate];
  48. }
  49. - (void)dealloc
  50. {
  51. [self.mapView removeObserver:self forKeyPath:@"delegate"];
  52. self.mapView.delegate = self.target;
  53. }
  54. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  55. {
  56. [self.mapView removeObserver:self forKeyPath:@"delegate"];
  57. [self swapDelegates];
  58. }
  59. - (void)swapDelegates
  60. {
  61. self.target = self.mapView.delegate;
  62. self.mapView.delegate = self;
  63. [self.mapView addObserver:self forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:NULL];
  64. }
  65. - (BOOL)respondsToSelector:(SEL)selector
  66. {
  67. // Check if selector is implemented in this class
  68. if ([super respondsToSelector:selector]) {
  69. return YES;
  70. }
  71. // Otherwise, use forwardInvocation: on delegates and target
  72. for (id delegate in self.delegates) {
  73. if ([delegate respondsToSelector:selector]) {
  74. return YES;
  75. }
  76. }
  77. return [self.target respondsToSelector:selector];
  78. }
  79. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
  80. {
  81. for (id delegate in self.delegates) {
  82. if ([delegate respondsToSelector:selector]) {
  83. return [delegate methodSignatureForSelector:selector];
  84. }
  85. }
  86. return [self.target methodSignatureForSelector:selector];
  87. }
  88. - (void)forwardInvocation:(NSInvocation *)invocation
  89. {
  90. for (id delegate in self.delegates) {
  91. if ([delegate respondsToSelector:invocation.selector]) {
  92. [invocation invokeWithTarget:delegate];
  93. }
  94. }
  95. if ([self.target respondsToSelector:invocation.selector]) {
  96. [invocation invokeWithTarget:self.target];
  97. }
  98. }
  99. #pragma mark - Map view proxied delegate methods
  100. - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
  101. {
  102. MKOverlayRenderer *renderer;
  103. // Target can override return value
  104. if ([self.target respondsToSelector:@selector(mapView:rendererForOverlay:)]) {
  105. renderer = [self.target mapView:mapView rendererForOverlay:overlay];
  106. }
  107. // Default return value for debug polygons
  108. if (renderer == nil && [overlay isKindOfClass:CCHMapClusterControllerDebugPolygon.class]) {
  109. MKPolygonRenderer *polygonRenderer = [[MKPolygonRenderer alloc] initWithPolygon:(MKPolygon *)overlay];
  110. #if TARGET_OS_IPHONE
  111. UIColor *color = [UIColor.blueColor colorWithAlphaComponent:0.7];
  112. #else
  113. NSColor *color = [NSColor.blueColor colorWithAlphaComponent:0.7];
  114. #endif
  115. polygonRenderer.strokeColor = color;
  116. polygonRenderer.lineWidth = 1;
  117. renderer = polygonRenderer;
  118. }
  119. return renderer;
  120. }
  121. @end