forked from workshirt/WSCoachMarksView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSCoachMarksView.m
264 lines (222 loc) · 8.48 KB
/
WSCoachMarksView.m
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
//
// WSCoachMarksView.m
// Version 0.2
//
// Created by Dimitry Bentsionov on 4/1/13.
// Copyright (c) 2013 Workshirt, Inc. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "WSCoachMarksView.h"
static const CGFloat kAnimationDuration = 0.3f;
static const CGFloat kCutoutRadius = 2.0f;
static const CGFloat kMaxLblWidth = 230.0f;
static const CGFloat kLblSpacing = 35.0f;
static const BOOL kEnableContinueLabel = YES;
@implementation WSCoachMarksView {
CAShapeLayer *mask;
NSUInteger markIndex;
UILabel *lblContinue;
}
#pragma mark - Properties
@synthesize delegate;
@synthesize coachMarks;
@synthesize lblCaption;
@synthesize maskColor = _maskColor;
@synthesize animationDuration;
@synthesize cutoutRadius;
@synthesize maxLblWidth;
@synthesize lblSpacing;
@synthesize enableContinueLabel;
#pragma mark - Methods
- (id)initWithFrame:(CGRect)frame coachMarks:(NSArray *)marks {
self = [super initWithFrame:frame];
if (self) {
// Save the coach marks
self.coachMarks = marks;
// Setup
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Setup
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Setup
[self setup];
}
return self;
}
- (void)setup {
// Default
self.animationDuration = kAnimationDuration;
self.cutoutRadius = kCutoutRadius;
self.maxLblWidth = kMaxLblWidth;
self.lblSpacing = kLblSpacing;
self.enableContinueLabel = kEnableContinueLabel;
// Shape layer mask
mask = [CAShapeLayer layer];
[mask setFillRule:kCAFillRuleEvenOdd];
[mask setFillColor:[[UIColor colorWithHue:0.0f saturation:0.0f brightness:0.0f alpha:0.9f] CGColor]];
[self.layer addSublayer:mask];
// Capture touches
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userDidTap:)];
[self addGestureRecognizer:tapGestureRecognizer];
// Captions
self.lblCaption = [[UILabel alloc] initWithFrame:(CGRect){{0.0f, 0.0f}, {self.maxLblWidth, 0.0f}}];
self.lblCaption.backgroundColor = [UIColor clearColor];
self.lblCaption.textColor = [UIColor whiteColor];
self.lblCaption.font = [UIFont systemFontOfSize:20.0f];
self.lblCaption.lineBreakMode = NSLineBreakByWordWrapping;
self.lblCaption.numberOfLines = 0;
self.lblCaption.textAlignment = NSTextAlignmentCenter;
self.lblCaption.alpha = 0.0f;
[self addSubview:self.lblCaption];
// Hide until unvoked
self.hidden = YES;
}
#pragma mark - Cutout modify
- (void)setCutoutToRect:(CGRect)rect {
// Define shape
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds];
UIBezierPath *cutoutPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:self.cutoutRadius];
[maskPath appendPath:cutoutPath];
// Set the new path
mask.path = maskPath.CGPath;
}
- (void)animateCutoutToRect:(CGRect)rect {
// Define shape
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds];
UIBezierPath *cutoutPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:self.cutoutRadius];
[maskPath appendPath:cutoutPath];
// Animate it
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
anim.delegate = self;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
anim.duration = self.animationDuration;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.fromValue = (__bridge id)(mask.path);
anim.toValue = (__bridge id)(maskPath.CGPath);
[mask addAnimation:anim forKey:@"path"];
mask.path = maskPath.CGPath;
}
#pragma mark - Mask color
- (void)setMaskColor:(UIColor *)maskColor {
_maskColor = maskColor;
[mask setFillColor:[maskColor CGColor]];
}
#pragma mark - Touch handler
- (void)userDidTap:(UITapGestureRecognizer *)recognizer {
// Go to the next coach mark
[self goToCoachMarkIndexed:(markIndex+1)];
}
#pragma mark - Navigation
- (void)start {
// Fade in self
self.alpha = 0.0f;
self.hidden = NO;
[UIView animateWithDuration:self.animationDuration
animations:^{
self.alpha = 1.0f;
}
completion:^(BOOL finished) {
// Go to the first coach mark
[self goToCoachMarkIndexed:0];
}];
}
- (void)goToCoachMarkIndexed:(NSUInteger)index {
// Out of bounds
if (index >= self.coachMarks.count) {
[self cleanup];
return;
}
// Current index
markIndex = index;
// Coach mark definition
NSDictionary *markDef = [self.coachMarks objectAtIndex:index];
NSString *markCaption = [markDef objectForKey:@"caption"];
CGRect markRect = [[markDef objectForKey:@"rect"] CGRectValue];
// Delegate (coachMarksView:willNavigateTo:atIndex:)
if ([self.delegate respondsToSelector:@selector(coachMarksView:willNavigateToIndex:)]) {
[self.delegate coachMarksView:self willNavigateToIndex:markIndex];
}
// Calculate the caption position and size
self.lblCaption.alpha = 0.0f;
self.lblCaption.frame = (CGRect){{0.0f, 0.0f}, {self.maxLblWidth, 0.0f}};
self.lblCaption.text = markCaption;
[self.lblCaption sizeToFit];
CGFloat y = markRect.origin.y + markRect.size.height + self.lblSpacing;
CGFloat bottomY = y + self.lblCaption.frame.size.height + self.lblSpacing;
if (bottomY > self.bounds.size.height) {
y = markRect.origin.y - self.lblSpacing - self.lblCaption.frame.size.height;
}
CGFloat x = floorf((self.bounds.size.width - self.lblCaption.frame.size.width) / 2.0f);
// Animate the caption label
self.lblCaption.frame = (CGRect){{x, y}, self.lblCaption.frame.size};
[UIView animateWithDuration:0.3f animations:^{
self.lblCaption.alpha = 1.0f;
}];
// If first mark, set the cutout to the center of first mark
if (markIndex == 0) {
CGPoint center = CGPointMake(floorf(markRect.origin.x + (markRect.size.width / 2.0f)), floorf(markRect.origin.y + (markRect.size.height / 2.0f)));
CGRect centerZero = (CGRect){center, CGSizeZero};
[self setCutoutToRect:centerZero];
}
// Animate the cutout
[self animateCutoutToRect:markRect];
// Show continue lbl if first mark
if (self.enableContinueLabel) {
if (markIndex == 0) {
lblContinue = [[UILabel alloc] initWithFrame:(CGRect){{0, self.bounds.size.height - 30.0f}, {self.bounds.size.width, 30.0f}}];
lblContinue.font = [UIFont boldSystemFontOfSize:13.0f];
lblContinue.textAlignment = NSTextAlignmentCenter;
lblContinue.text = @"点击继续";
lblContinue.alpha = 0.0f;
lblContinue.backgroundColor = [UIColor whiteColor];
[self addSubview:lblContinue];
[UIView animateWithDuration:0.3f delay:1.0f options:0 animations:^{
lblContinue.alpha = 1.0f;
} completion:nil];
} else if (markIndex > 0 && lblContinue != nil) {
// Otherwise, remove the lbl
[lblContinue removeFromSuperview];
lblContinue = nil;
}
}
}
#pragma mark - Cleanup
- (void)cleanup {
// Delegate (coachMarksViewWillCleanup:)
if ([self.delegate respondsToSelector:@selector(coachMarksViewWillCleanup:)]) {
[self.delegate coachMarksViewWillCleanup:self];
}
// Fade out self
[UIView animateWithDuration:self.animationDuration
animations:^{
self.alpha = 0.0f;
}
completion:^(BOOL finished) {
// Remove self
[self removeFromSuperview];
// Delegate (coachMarksViewDidCleanup:)
if ([self.delegate respondsToSelector:@selector(coachMarksViewDidCleanup:)]) {
[self.delegate coachMarksViewDidCleanup:self];
}
}];
}
#pragma mark - Animation delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
// Delegate (coachMarksView:didNavigateTo:atIndex:)
if ([self.delegate respondsToSelector:@selector(coachMarksView:didNavigateToIndex:)]) {
[self.delegate coachMarksView:self didNavigateToIndex:markIndex];
}
}
@end