Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 3.31 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. #import <Foundation/Foundation.h>
  4. #import <MapKit/MapKit.h>
  5. #import "MyAnnotation.h"
  6.  
  7. @interface MyAnnotationView : MKAnnotationView {
  8.  
  9.         UIImageView     *calloutView;
  10.         CGSize          calloutViewSize;
  11.         BOOL            useCustomCalloutView;
  12.         BOOL            canShowCallout;
  13. }
  14.  
  15. - (void)showCalloutView:(id)sender;
  16. - (void)hideCalloutView:(id)sender;
  17. - (BOOL) canShowCallout;
  18. - (void) setCanShowCallout: (BOOL) newValue;
  19.  
  20. @property (nonatomic,retain) UIImageView        *calloutView;
  21. @property (nonatomic) CGSize            calloutViewSize;
  22. @property (nonatomic) BOOL                      useCustomCalloutView;
  23.  
  24. @end
  25.  
  26. #import "MyAnnotationView.h"
  27.  
  28. @implementation MyAnnotationView
  29. @synthesize calloutView, calloutViewSize, useCustomCalloutView;
  30.  
  31. - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
  32.         self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  33.         if (self != nil) {
  34.                 self.image  = [UIImage imageNamed:@"icon.png"];
  35.                
  36.                 calloutView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width/2,self.bounds.origin.y,0,0)];
  37.                 calloutView.image = [[UIImage imageNamed:@"speech.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
  38.                 //default size - set as property annotationView.calloutViewSize = CGMake(blah, blah);
  39.                 calloutViewSize = CGSizeMake(200, 100);
  40.                
  41.                 //set to no to start as defualt
  42.                 self.useCustomCalloutView = NO;
  43.                
  44.                 [self addSubview:calloutView];
  45.                
  46.         }
  47.         return self;
  48. }
  49.  
  50. //override so use cannot set them
  51. - (BOOL) canShowCallout {
  52.         return NO;
  53. }
  54.  
  55. - (void) setCanShowCallout: (BOOL) newValue {
  56.         canShowCallout = NO;
  57. }
  58.  
  59. -(void)setSelected:(BOOL)_selected {
  60.        
  61.         if (self.useCustomCalloutView) {
  62.                 if (_selected) {
  63.                         [self showCalloutView:nil];
  64.                 } else {
  65.                         [self hideCalloutView:nil];
  66.                 }
  67.         }
  68.        
  69.         [super setSelected:_selected];
  70.        
  71. }
  72.  
  73.  
  74. - (void)showCalloutView:(id)sender {
  75.        
  76.         [UIView beginAnimations: nil context: nil];
  77.         [UIView setAnimationDuration: 0.3];
  78.         [UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
  79.        
  80.         calloutView.frame = CGRectMake((self.bounds.size.width-calloutViewSize.width)/2 + self.calloutOffset.x,
  81.                                                                    self.bounds.origin.y - calloutViewSize.height + self.calloutOffset.y,
  82.                                                                    calloutViewSize.width,
  83.                                                                    calloutViewSize.height);
  84.        
  85.         [UIView commitAnimations];     
  86.        
  87. }
  88.  
  89. - (void)hideCalloutView:(id)sender {
  90.        
  91.         [UIView beginAnimations: nil context: nil];
  92.         [UIView setAnimationDuration: 0.1];
  93.         [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
  94.        
  95.         calloutView.frame = CGRectMake(self.bounds.size.width/2, self.bounds.origin.y,0,0);
  96.        
  97.         [UIView commitAnimations];
  98. }
  99.  
  100.  
  101. - (void)dealloc {
  102.         [calloutView release];
  103.     [super dealloc];
  104. }
  105.  
  106.  
  107. @end
  108.  
  109.  
  110.  
  111. /*
  112.  
  113. USAGE
  114. - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
  115. {
  116.     static NSString *annotationViewID = @"PinID";
  117.         MyAnnotation *myAnnotation = (MyAnnotation*) annotation;
  118.        
  119.     MyAnnotationView *annotationView =
  120.         (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
  121.     if (annotationView == nil)
  122.     {
  123.         annotationView = [[[MyAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:annotationViewID] autorelease];
  124.                 annotationView.canShowCallout = YES;
  125.                 annotationView.useCustomCalloutView = YES;
  126.     }
  127.        
  128.     return annotationView;
  129. }