-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathJPSThumbnailAnnotationView.m
More file actions
executable file
·293 lines (233 loc) · 11.6 KB
/
JPSThumbnailAnnotationView.m
File metadata and controls
executable file
·293 lines (233 loc) · 11.6 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// JPSThumbnailAnnotationView.m
// JPSThumbnailAnnotationView
//
// Created by Jean-Pierre Simard on 4/21/13.
// Copyright (c) 2013 JP Simard. All rights reserved.
//
@import QuartzCore;
#import "JPSThumbnailAnnotationView.h"
#import "JPSThumbnail.h"
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"
NSString * const kJPSThumbnailAnnotationViewReuseID = @"JPSThumbnailAnnotationView";
static CGFloat const kJPSThumbnailAnnotationViewStandardWidth = 75.0f;
static CGFloat const kJPSThumbnailAnnotationViewStandardHeight = 87.0f;
static CGFloat const kJPSThumbnailAnnotationViewExpandOffset = 200.0f;
static CGFloat const kJPSThumbnailAnnotationViewVerticalOffset = 34.0f;
static CGFloat const kJPSThumbnailAnnotationViewAnimationDuration = 0.25f;
@interface JPSThumbnailAnnotationView ()
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) ActionBlock disclosureBlock;
@property (nonatomic, strong) CAShapeLayer *bgLayer;
@property (nonatomic, strong) UIButton *disclosureButton;
@property (nonatomic, assign) JPSThumbnailAnnotationViewState state;
@end
@implementation JPSThumbnailAnnotationView
#pragma mark - Setup
- (id)initWithAnnotation:(id<MKAnnotation>)annotation {
self = [super initWithAnnotation:annotation reuseIdentifier:kJPSThumbnailAnnotationViewReuseID];
if (self) {
self.canShowCallout = NO;
self.frame = CGRectMake(0, 0, kJPSThumbnailAnnotationViewStandardWidth, kJPSThumbnailAnnotationViewStandardHeight);
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(0, -kJPSThumbnailAnnotationViewVerticalOffset);
_state = JPSThumbnailAnnotationViewStateCollapsed;
[self setupView];
}
return self;
}
- (void)setupView {
[self setupImageView];
[self setupTitleLabel];
[self setupSubtitleLabel];
[self setupDisclosureButton];
[self setLayerProperties];
[self setDetailGroupAlpha:0.0f];
}
- (void)setupImageView {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12.5f, 12.5f, 50.0f, 47.0f)];
_imageView.layer.cornerRadius = 4.0f;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
_imageView.layer.borderWidth = 0.5f;
[self addSubview:_imageView];
}
- (void)setupTitleLabel {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-32.0f, 16.0f, 168.0f, 20.0f)];
_titleLabel.textColor = [UIColor darkTextColor];
_titleLabel.font = [UIFont boldSystemFontOfSize:17];
_titleLabel.minimumScaleFactor = 0.8f;
_titleLabel.adjustsFontSizeToFitWidth = YES;
[self addSubview:_titleLabel];
}
- (void)setupSubtitleLabel {
_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-32.0f, 36.0f, 168.0f, 20.0f)];
_subtitleLabel.textColor = [UIColor grayColor];
_subtitleLabel.font = [UIFont systemFontOfSize:12.0f];
[self addSubview:_subtitleLabel];
}
- (void)setupDisclosureButton {
BOOL iOS7 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f;
UIButtonType buttonType = iOS7 ? UIButtonTypeSystem : UIButtonTypeCustom;
_disclosureButton = [UIButton buttonWithType:buttonType];
_disclosureButton.tintColor = [UIColor grayColor];
UIImage *disclosureIndicatorImage = [JPSThumbnailAnnotationView disclosureButtonImage];
[_disclosureButton setImage:disclosureIndicatorImage forState:UIControlStateNormal];
_disclosureButton.frame = CGRectMake(kJPSThumbnailAnnotationViewExpandOffset/2.0f + self.frame.size.width/2.0f + 8.0f,
26.5f,
disclosureIndicatorImage.size.width,
disclosureIndicatorImage.size.height);
[_disclosureButton addTarget:self action:@selector(didTapDisclosureButton) forControlEvents:UIControlEventTouchDown];
[self addSubview:_disclosureButton];
}
- (void)setLayerProperties {
_bgLayer = [CAShapeLayer layer];
CGPathRef path = [self newBubbleWithRect:self.bounds];
_bgLayer.path = path;
CFRelease(path);
_bgLayer.fillColor = [UIColor whiteColor].CGColor;
_bgLayer.shadowColor = [UIColor blackColor].CGColor;
_bgLayer.shadowOffset = CGSizeMake(0.0f, 3.0f);
_bgLayer.shadowRadius = 2.0f;
_bgLayer.shadowOpacity = 0.5f;
_bgLayer.masksToBounds = NO;
[self.layer insertSublayer:_bgLayer atIndex:0];
}
#pragma mark - Updating
- (void)updateWithThumbnail:(JPSThumbnail *)thumbnail {
self.coordinate = thumbnail.coordinate;
self.titleLabel.text = thumbnail.title;
self.subtitleLabel.text = thumbnail.subtitle;
[self assignImageWithThumbnail:thumbnail];
self.disclosureBlock = thumbnail.disclosureBlock;
}
-(void)assignImageWithThumbnail:(JPSThumbnail *)thumbnail {
//figure out if we Should directly set image or use URL to download it instead
if (thumbnail.image) {
self.imageView.image = thumbnail.image;
}else if(thumbnail.imageURL){
[self.imageView setImageWithURL:thumbnail.imageURL placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
}
}
#pragma mark - JPSThumbnailAnnotationViewProtocol
- (void)didSelectAnnotationViewInMap:(MKMapView *)mapView {
// Center map at annotation point
[mapView setCenterCoordinate:self.coordinate animated:YES];
[self expand];
}
- (void)didDeselectAnnotationViewInMap:(MKMapView *)mapView {
[self shrink];
}
#pragma mark - Geometry
- (CGPathRef)newBubbleWithRect:(CGRect)rect {
CGFloat stroke = 1.0f;
CGFloat radius = 7.0f;
CGMutablePathRef path = CGPathCreateMutable();
CGFloat parentX = rect.origin.x + rect.size.width/2.0f;
// Determine Size
rect.size.width -= stroke + 14.0f;
rect.size.height -= stroke + 29.0f;
rect.origin.x += stroke / 2.0f + 7.0f;
rect.origin.y += stroke / 2.0f + 7.0f;
// Create Callout Bubble Path
CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y + radius);
CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height - radius);
CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI_2, 1);
CGPathAddLineToPoint(path, NULL, parentX - 14.0f, rect.origin.y + rect.size.height);
CGPathAddLineToPoint(path, NULL, parentX, rect.origin.y + rect.size.height + 14.0f);
CGPathAddLineToPoint(path, NULL, parentX + 14.0f, rect.origin.y + rect.size.height);
CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height);
CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI_2, 0.0f, 1.0f);
CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + radius);
CGPathAddArc(path, NULL, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, radius, 0.0f, -M_PI_2, 1.0f);
CGPathAddLineToPoint(path, NULL, rect.origin.x + radius, rect.origin.y);
CGPathAddArc(path, NULL, rect.origin.x + radius, rect.origin.y + radius, radius, -M_PI_2, M_PI, 1.0f);
CGPathCloseSubpath(path);
return path;
}
#pragma mark - Animations
- (void)setDetailGroupAlpha:(CGFloat)alpha {
self.disclosureButton.alpha = alpha;
self.titleLabel.alpha = alpha;
self.subtitleLabel.alpha = alpha;
}
- (void)expand {
if (self.state != JPSThumbnailAnnotationViewStateCollapsed) return;
self.state = JPSThumbnailAnnotationViewStateAnimating;
[self animateBubbleWithDirection:JPSThumbnailAnnotationViewAnimationDirectionGrow];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width+kJPSThumbnailAnnotationViewExpandOffset, self.frame.size.height);
self.centerOffset = CGPointMake(kJPSThumbnailAnnotationViewExpandOffset/2.0f, -kJPSThumbnailAnnotationViewVerticalOffset);
[UIView animateWithDuration:kJPSThumbnailAnnotationViewAnimationDuration/2.0f delay:kJPSThumbnailAnnotationViewAnimationDuration options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self setDetailGroupAlpha:1.0f];
} completion:^(BOOL finished) {
self.state = JPSThumbnailAnnotationViewStateExpanded;
}];
}
- (void)shrink {
if (self.state != JPSThumbnailAnnotationViewStateExpanded) return;
self.state = JPSThumbnailAnnotationViewStateAnimating;
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width - kJPSThumbnailAnnotationViewExpandOffset,
self.frame.size.height);
[UIView animateWithDuration:kJPSThumbnailAnnotationViewAnimationDuration/2.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self setDetailGroupAlpha:0.0f];
}
completion:^(BOOL finished) {
[self animateBubbleWithDirection:JPSThumbnailAnnotationViewAnimationDirectionShrink];
self.centerOffset = CGPointMake(0.0f, -kJPSThumbnailAnnotationViewVerticalOffset);
}];
}
- (void)animateBubbleWithDirection:(JPSThumbnailAnnotationViewAnimationDirection)animationDirection {
BOOL growing = (animationDirection == JPSThumbnailAnnotationViewAnimationDirectionGrow);
// Image
[UIView animateWithDuration:kJPSThumbnailAnnotationViewAnimationDuration animations:^{
CGFloat xOffset = (growing ? -1 : 1) * kJPSThumbnailAnnotationViewExpandOffset/2.0f;
self.imageView.frame = CGRectOffset(self.imageView.frame, xOffset, 0.0f);
} completion:^(BOOL finished) {
if (animationDirection == JPSThumbnailAnnotationViewAnimationDirectionShrink) {
self.state = JPSThumbnailAnnotationViewStateCollapsed;
}
}];
// Bubble
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = 1;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.duration = kJPSThumbnailAnnotationViewAnimationDuration;
// Stroke & Shadow From/To Values
CGRect largeRect = CGRectInset(self.bounds, -kJPSThumbnailAnnotationViewExpandOffset/2.0f, 0.0f);
CGPathRef fromPath = [self newBubbleWithRect:growing ? self.bounds : largeRect];
animation.fromValue = (__bridge id)fromPath;
CGPathRelease(fromPath);
CGPathRef toPath = [self newBubbleWithRect:growing ? largeRect : self.bounds];
animation.toValue = (__bridge id)toPath;
CGPathRelease(toPath);
[self.bgLayer addAnimation:animation forKey:animation.keyPath];
}
#pragma mark - Disclosure Button
- (void)didTapDisclosureButton {
if (self.disclosureBlock) self.disclosureBlock();
}
+ (UIImage *)disclosureButtonImage {
CGSize size = CGSizeMake(21.0f, 36.0f);
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(2.0f, 2.0f)];
[bezierPath addLineToPoint:CGPointMake(10.0f, 10.0f)];
[bezierPath addLineToPoint:CGPointMake(2.0f, 18.0f)];
[[UIColor lightGrayColor] setStroke];
bezierPath.lineWidth = 3.0f;
[bezierPath stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end