-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCEGuideArrow.m
More file actions
284 lines (210 loc) · 9.45 KB
/
CEGuideArrow.m
File metadata and controls
284 lines (210 loc) · 9.45 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
//
// CEGuideArrow.m
//
// Created by Chad Etzel on 7/22/12.
// Copyright (c) <2012> Chad Etzel
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#import "CEGuideArrow.h"
@implementation CEGuideArrow
@synthesize delegate = _delegate;
@synthesize displayed = _displayed;
static CEGuideArrow *_sharedGuideArrow;
+ (CEGuideArrow *)sharedGuideArrow
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedGuideArrow = [[CEGuideArrow alloc] init];
});
return _sharedGuideArrow;
}
- (id)init
{
return [self initWithArrowImage:[self defaultArrowImage] anchorPoint:[self defaultAnchorPoint]];
}
- (id)initWithArrowImage:(UIImage *)arrowImage anchorPoint:(CGPoint)anchorPoint
{
if (self = [super init]) {
_arrowView = [[UIImageView alloc] initWithImage:arrowImage];
_origSize = _arrowView.frame.size;
_arrowView.layer.anchorPoint = anchorPoint;
}
return self;
}
- (void)dealloc
{
[_arrowView release];
[super dealloc];
}
- (UIImage *)defaultArrowImage
{
// subclasses should return whatever image they want to use by default
return [UIImage imageNamed:@"GuideArrow"];
}
- (CGPoint)defaultAnchorPoint
{
//subclasses should return the anchor point to use for their default image
return CGPointMake(1.0, 0.5);
}
- (void)removeCompleted
{
[_arrowView removeFromSuperview];
if ([self.delegate respondsToSelector:@selector(ceGuideArrow:didDisappearInWindow:atPoint:inView:length:)]) {
[self.delegate ceGuideArrow:self didDisappearInWindow:_window atPoint:_point inView:_view length:_length];
}
_displayed = NO;
[_arrowView.layer removeAnimationForKey:@"bounceAnimation"];
}
- (void)removeAnimated:(BOOL)animated
{
if ([self.delegate respondsToSelector:@selector(ceGuideArrow:willDisappearInWindow:atPoint:inView:length:)]) {
[self.delegate ceGuideArrow:self willDisappearInWindow:_window atPoint:_point inView:_view length:_length];
}
if (animated) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionAllowUserInteraction animations:^{
[_arrowView setAlpha:0.0];
} completion:^(BOOL finished) {
[self removeCompleted];
}];
} else {
[self removeCompleted];
}
}
- (void)showInWindow:(UIWindow *)window atPoint:(CGPoint)point inView:(UIView *)view atAngle:(CGFloat)degrees length:(CGFloat)length
{
if ([self.delegate respondsToSelector:@selector(ceGuideArrow:shouldShowArrowInWindow:atPoint:inView:length:)]) {
if (![self.delegate ceGuideArrow:self shouldShowArrowInWindow:window atPoint:point inView:view length:length]) {
return;
}
}
_window = window;
_view = view;
_point = point;
_length = length;
if ([self.delegate respondsToSelector:@selector(ceGuideArrow:willAppearInWindow:atPoint:inView:length:)]) {
[self.delegate ceGuideArrow:self willAppearInWindow:_window atPoint:_point inView:_view length:_length];
}
CGPoint windowPoint = [window convertPoint:point fromView:view];
CGRect windowRect = [[view superview] convertRect:view.frame toView:window];
windowPoint = CGPointMake(windowRect.origin.x + point.x, windowRect.origin.y + point.y);
//// bounce animation
[_arrowView.layer removeAnimationForKey:@"bounceAnimation"];
CABasicAnimation *yAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
yAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
yAnimation.fromValue = [NSNumber numberWithFloat:0];
yAnimation.toValue = [NSNumber numberWithFloat:0.0 - 10.0 * sin(M_PI/180.0 * degrees)];
yAnimation.repeatCount = INT_MAX;
yAnimation.autoreverses = YES;
yAnimation.fillMode = kCAFillModeForwards;
yAnimation.removedOnCompletion = NO;
yAnimation.additive = YES;
CABasicAnimation *xAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
xAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
xAnimation.fromValue = [NSNumber numberWithFloat:0];
xAnimation.toValue = [NSNumber numberWithFloat:0.0 - 10.0 * cos(M_PI/180.0 * degrees)];
xAnimation.repeatCount = INT_MAX;
xAnimation.autoreverses = YES;
xAnimation.fillMode = kCAFillModeForwards;
xAnimation.removedOnCompletion = NO;
xAnimation.additive = YES;
CAAnimationGroup *bounceAnimation = [CAAnimationGroup animation];
bounceAnimation.fillMode = kCAFillModeForwards;
bounceAnimation.removedOnCompletion = NO;
[bounceAnimation setAnimations:[NSArray arrayWithObjects:yAnimation, xAnimation, nil]];
bounceAnimation.duration = 0.25;
bounceAnimation.repeatCount = INT_MAX;
bounceAnimation.autoreverses = YES;
[_arrowView.layer addAnimation:bounceAnimation forKey:@"bounceAnimation"];
if (!_displayed) {
_arrowView.alpha = 0.0;
[window addSubview:_arrowView];
}
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
[_arrowView setAlpha:1.0];
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(M_PI/180.0 * degrees);
if (length == 0.0) {
_arrowView.transform = rotateTransform;
_arrowView.transform = CGAffineTransformScale(rotateTransform, 1.0, 1.0);
} else {
CGFloat ratio = length / _origSize.width;
_arrowView.transform = CGAffineTransformScale(rotateTransform, ratio, ratio);
}
_arrowView.layer.position = windowPoint;
} completion:^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(ceGuideArrow:didAppearInWindow:atPoint:inView:length:)]) {
[self.delegate ceGuideArrow:self didAppearInWindow:_window atPoint:_point inView:_view length:_length];
}
_displayed = YES;
}];
}
- (void)showInWindow:(UIWindow *)window atPoint:(CGPoint)point inView:(UIView *)view atAngle:(CGFloat)degrees
{
[self showInWindow:window atPoint:point inView:view atAngle:degrees length:0.0];
}
- (void)showInWindow:(UIWindow *)window atPosition:(CEGuideArrowPositionType)position inView:(UIView *)view atAngle:(CGFloat)degrees length:(CGFloat)length
{
CGPoint point;
switch (position) {
case CEGuideArrowPositionTypeTopLeft:
point = CGPointMake(0.0, 0.0);
break;
case CEGuideArrowPositionTypeTopRight:
point = CGPointMake(view.frame.size.width, 0.0);
break;
case CEGuideArrowPositionTypeBottomLeft:
point = CGPointMake(0.0, view.frame.size.height);
break;
case CEGuideArrowPositionTypeBottomRight:
point = CGPointMake(view.frame.size.width, view.frame.size.height);
break;
case CEGuideArrowPositionTypeTopCenter:
point = CGPointMake(ceilf(view.frame.size.width / 2.0), 0.0);
break;
case CEGuideArrowPositionTypeBottomCenter:
point = CGPointMake(ceilf(view.frame.size.width / 2.0), view.frame.size.height);
break;
case CEGuideArrowPositionTypeLeftCenter:
point = CGPointMake(0.0, ceilf(view.frame.size.height / 2.0));
break;
case CEGuideArrowPositionTypeRightCenter:
point = CGPointMake(view.frame.size.width, ceilf(view.frame.size.height / 2.0));
break;
case CEGuideArrowPositionTypeCenter:
point = CGPointMake(ceilf(view.frame.size.width / 2.0), ceilf(view.frame.size.height / 2.0));
break;
default:
point = CGPointMake(0.0, 0.0);
break;
}
[self showInWindow:window atPoint:point inView:view atAngle:degrees length:length];
}
- (void)showInWindow:(UIWindow *)window atPosition:(CEGuideArrowPositionType)position inView:(UIView *)view atAngle:(CGFloat)degrees
{
[self showInWindow:window atPosition:position inView:view atAngle:degrees length:0.0];
}
- (UIView *)arrowView
{
return _arrowView;
}
@end