|
| 1 | +// |
| 2 | +// CustomAnnotationView.m |
| 3 | +// GeoPOC |
| 4 | +// |
| 5 | +// Created by raw engineering, inc on 8/17/12. |
| 6 | +// Copyright (c) 2012 raw engineering, inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CustomAnnotationView.h" |
| 10 | +#import "Annotation.h" |
| 11 | +#import "AppConstants.h" |
| 12 | + |
| 13 | +@implementation CustomAnnotationView |
| 14 | + |
| 15 | +@synthesize calloutView; |
| 16 | +@synthesize endFrame; |
| 17 | +@synthesize delegate; |
| 18 | +@synthesize shareButton; |
| 19 | +@synthesize dateLabel; |
| 20 | +@synthesize clearButton; |
| 21 | + |
| 22 | +- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ |
| 23 | + [super setSelected:selected animated:animated]; |
| 24 | + Annotation *ann = self.annotation; |
| 25 | + if(selected) |
| 26 | + { |
| 27 | + //Add your custom view to self... |
| 28 | + calloutView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"graphic_mapCallout.png"]]; |
| 29 | + [calloutView setFrame:CGRectMake(-46, 35, 0, 0)]; |
| 30 | + [calloutView setUserInteractionEnabled:YES]; |
| 31 | + [calloutView sizeToFit]; |
| 32 | + |
| 33 | + CGFloat bottom; |
| 34 | + |
| 35 | + if ([ann.userType isEqualToString:SENDER]) { |
| 36 | + shareButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
| 37 | + [shareButton setTitle:SHARE forState:UIControlStateNormal]; |
| 38 | + [shareButton setFrame:CGRectMake(12, 25, 88, 25)]; |
| 39 | + [shareButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; |
| 40 | + [shareButton setTag:0]; |
| 41 | + [calloutView addSubview:shareButton]; |
| 42 | + bottom = shareButton.frame.origin.y + shareButton.frame.size.height + 7; |
| 43 | + }else if([ann.userType isEqualToString:RECEIVER]) { |
| 44 | + dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 25, 88, 30)]; |
| 45 | + |
| 46 | + NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; |
| 47 | + [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZZZZ"]; |
| 48 | + |
| 49 | + NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init]; |
| 50 | + [formatter2 setDateFormat:@"MM/dd/yyyy h:mm a"]; |
| 51 | + |
| 52 | + [dateLabel setText:[NSString stringWithFormat:@"Sent %@",[formatter2 stringFromDate:[formatter dateFromString:[ann.date stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]]; |
| 53 | + |
| 54 | + formatter = nil; |
| 55 | + formatter2 = nil; |
| 56 | + |
| 57 | + [dateLabel setFont:[UIFont boldSystemFontOfSize:11.5]]; |
| 58 | + [dateLabel setBackgroundColor:[UIColor clearColor]]; |
| 59 | + [dateLabel setTextColor:[UIColor whiteColor]]; |
| 60 | + [dateLabel setTextAlignment:UITextAlignmentCenter]; |
| 61 | + [calloutView addSubview:dateLabel]; |
| 62 | + [dateLabel setNumberOfLines:2]; |
| 63 | + |
| 64 | + bottom = dateLabel.frame.origin.y + dateLabel.frame.size.height + 7; |
| 65 | + } |
| 66 | + |
| 67 | + clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
| 68 | + [clearButton setTitle:CLEAR forState:UIControlStateNormal]; |
| 69 | + [clearButton setFrame:CGRectMake(12, bottom, 88, 25)]; |
| 70 | + [clearButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown]; |
| 71 | + [clearButton setTag:1]; |
| 72 | + [calloutView addSubview:clearButton]; |
| 73 | + |
| 74 | + [self animateCalloutAppearance]; |
| 75 | + [self addSubview:calloutView]; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + //Remove your custom view... |
| 80 | + [calloutView removeFromSuperview]; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +- (void)buttonClicked:(id)sender{ |
| 85 | + UIButton *button = (UIButton *)sender; |
| 86 | + [delegate didSelectButtonAtIndex:button.tag forAnnotation:self.annotation]; |
| 87 | +} |
| 88 | + |
| 89 | +- (void)didAddSubview:(UIView *)subview{ |
| 90 | + if ([[[subview class] description] isEqualToString:@"UICalloutView"]) { |
| 91 | + for (UIView *subsubView in subview.subviews) { |
| 92 | + if ([subsubView class] == [UIImageView class]) { |
| 93 | + UIImageView *imageView = ((UIImageView *)subsubView); |
| 94 | + [imageView removeFromSuperview]; |
| 95 | + }else if ([subsubView class] == [UILabel class]) { |
| 96 | + UILabel *labelView = ((UILabel *)subsubView); |
| 97 | + [labelView removeFromSuperview]; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +- (void)animateCalloutAppearance { |
| 104 | + CGFloat scale = 0.001f; |
| 105 | + calloutView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, 0, -50); |
| 106 | + |
| 107 | + [UIView animateWithDuration:0.12f delay:0 options:UIViewAnimationCurveEaseOut animations:^{ |
| 108 | + CGFloat scale = 1.05f; |
| 109 | + calloutView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, 0, 2); |
| 110 | + } completion:^(BOOL finished) { |
| 111 | + [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ |
| 112 | + CGFloat scale = 0.95; |
| 113 | + calloutView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, 0, -2); |
| 114 | + } completion:^(BOOL finished) { |
| 115 | + [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ |
| 116 | + CGFloat scale = 1.0; |
| 117 | + calloutView.transform = CGAffineTransformMake(scale, 0.0f, 0.0f, scale, 0, 0); |
| 118 | + } completion:nil]; |
| 119 | + }]; |
| 120 | + }]; |
| 121 | +} |
| 122 | + |
| 123 | +-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ |
| 124 | +// if (CGRectContainsPoint(calloutView.frame, point)) { |
| 125 | +// NSLog(@"YES"); |
| 126 | +// } |
| 127 | +// NSLog(@"Point %f",point.x); |
| 128 | + return YES; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +@end |
0 commit comments