CallOutAnnotationView.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // CallOutAnnotationView.m
  3. //
  4. // Created by Jian-Ye on 12-11-8.
  5. // Copyright (c) 2012年 Jian-Ye. All rights reserved.
  6. //
  7. #import "CallOutAnnotationView.h"
  8. #import <QuartzCore/QuartzCore.h>
  9. @interface CallOutAnnotationView ()
  10. @property (nonatomic,retain)id<CallOutAnnotationViewDelegate>delegate;
  11. @end
  12. @implementation CallOutAnnotationView
  13. - (id)initWithAnnotation:(id<MKAnnotation>)annotation
  14. reuseIdentifier:(NSString *)reuseIdentifier
  15. delegate:(id<CallOutAnnotationViewDelegate>)delegate
  16. {
  17. self = [super initWithAnnotation:annotation
  18. reuseIdentifier:reuseIdentifier];
  19. if (self) {
  20. self.backgroundColor = [UIColor clearColor];
  21. self.canShowCallout = NO;
  22. self.centerOffset = CGPointMake(0, -55);
  23. self.frame = CGRectMake(0, 0, 240, 80);
  24. if (delegate) {
  25. self.delegate = delegate;
  26. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
  27. [self addGestureRecognizer:tap];
  28. }
  29. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - Arror_height)];
  30. contentView.backgroundColor = [UIColor clearColor];
  31. [self addSubview:contentView];
  32. self.contentView = contentView;
  33. }
  34. return self;
  35. }
  36. - (void)tapAction
  37. {
  38. if ([_delegate respondsToSelector:@selector(didSelectAnnotationView:)]) {
  39. [_delegate didSelectAnnotationView:self];
  40. }
  41. }
  42. #pragma mark -
  43. #pragma mark draw
  44. - (void)getDrawPath:(CGContextRef)context rect:(CGRect)rect
  45. {
  46. CGRect rrect = rect;
  47. CGFloat radius = 6.0;
  48. CGFloat minx = CGRectGetMinX(rrect),
  49. midx = CGRectGetMidX(rrect),
  50. maxx = CGRectGetMaxX(rrect);
  51. CGFloat miny = CGRectGetMinY(rrect),
  52. maxy = CGRectGetMaxY(rrect)-Arror_height;
  53. CGContextMoveToPoint(context, midx+Arror_height, maxy);
  54. CGContextAddLineToPoint(context,midx, maxy+Arror_height);
  55. CGContextAddLineToPoint(context,midx-Arror_height, maxy);
  56. CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
  57. CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
  58. CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
  59. CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
  60. CGContextClosePath(context);
  61. }
  62. - (void)drawRect:(CGRect)rect
  63. {
  64. CGContextRef context = UIGraphicsGetCurrentContext();
  65. CGContextSetLineWidth(context, 2.0);
  66. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  67. [self getDrawPath:context rect:self.bounds];
  68. CGContextFillPath(context);
  69. CGPathRef path = CGContextCopyPath(context);
  70. self.layer.shadowColor = [UIColor blackColor].CGColor;
  71. self.layer.shadowOffset = CGSizeMake(0, 0);
  72. self.layer.shadowOpacity = 1;
  73. //insert
  74. self.layer.shadowPath = path;
  75. }
  76. @end