-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYCTutorialBox.m
261 lines (211 loc) · 8.19 KB
/
YCTutorialBox.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
//
// YCTutorialBox.m
// YU - YUPPIU
//
// Created by Fabio Knoedt on 26/08/14.
// Copyright (c) 2014 Datedicted. All rights reserved.
//
#import "YCTutorialBox.h"
#define distanceBetweenViewAndBox 20
@interface YCTutorialBox ()
/*!
* The box UIView containing the headline and helpText.
*/
@property (weak, nonatomic) IBOutlet UIView *box;
/*!
* The headline Outlet property.
*/
@property (weak, nonatomic) IBOutlet UILabel *boxHeadline;
/*!
* The helpText Outlet property.
*/
@property (weak, nonatomic) IBOutlet UILabel *boxHelpText;
/*!
* The view that should be in focus when displaying the box.
*/
@property (strong, nonatomic) UIView *viewInFocus;
/*!
* The block of code that should be executed after the user dismisses the box.
*/
@property (nonatomic, copy) void (^completionBlock)();
@end
/*!
* Implementation for YCTutorialBox.
*/
@implementation YCTutorialBox
/*!
* Initialize the view with a headline.
* @param headline The Headline of the box.
* @return the initialized object.
*/
- (instancetype)initWithHeadline:(NSString *)headline;
{
return [self initWithHeadline:headline withHelpText:nil withCompletionBlock:nil];
}
/*!
* Initialize the view with a headline and a help text.
* @param headline headline The Headline of the box.
* @param helpText headline The help text or description of the box.
* @return the initialized object.
*/
- (instancetype)initWithHeadline:(NSString *)headline
withHelpText:(NSString *)helpText;
{
return [self initWithHeadline:headline withHelpText:helpText withCompletionBlock:nil];
}
/*!
* Initialize the view with a headline, help text and a completion block.
* @param headline headline The Headline of the box.
* @param helpText headline The help text or description of the box.
* @param completion A completion block to be executed after the view is dismissed.
* @return the initialized object.
*/
- (instancetype)initWithHeadline:(NSString *)headline
withHelpText:(NSString *)helpText
withCompletionBlock:(void (^)())completion;
{
/// Init the view.
self = [[[NSBundle bundleForClass:[self class]] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
/// If we loaded the bundle and the .xib file just fine.
if (self) {
/// Set the texts.
_boxHeadline.text = headline;
_boxHelpText.text = helpText;
/// Set the completion block.
_completionBlock = completion;
}
return self;
}
/*!
* Show the box on the screen.
*/
- (void)show;
{
[self showAndFocusView:nil];
}
/*!
* Show the box on the screen with focus on the view selected.
* @param view The view to make focus on.
*/
- (void)showAndFocusView:(UIView *)view;
{
/// Add itself to the main window.
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:self];
/// Set the view to focus.
if (view) {
[self setViewInFocus:view];
[self setNeedsLayout];
}
}
#pragma mark - Private methods
/*!
* Position the box with the text in the best place: top/over/bottom/center.
* @param view The view that was snapshotted (in focus).
*/
- (void)positionBox:(UIView *)view;
{
/// If there is no view, display the box in the center of the screen.
if (view) {
/// Point in the window.
CGPoint pointInWindow = [self convertPoint:view.bounds.origin fromView:view];
/// Height between the view in focus and the top.
CGFloat heightUp = pointInWindow.y;
/// Height between the view in focus and the bottom.
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
CGFloat heightDown = currentWindow.frame.size.height - pointInWindow.y - view.frame.size.height;
/// Check what is the better place to place the box.
/// Prio 1: Place the box in front of the element, if it is big enough.
if (view.frame.size.height > 300.0f && view.frame.size.height > _box.frame.size.height) {
[_box setCenter:CGPointMake(_box.center.x, pointInWindow.y + view.frame.size.height/2)];
}
/// Prio 2: Place the box above the element.
else if (heightUp > heightDown && heightUp > _box.frame.size.height + 2*distanceBetweenViewAndBox) {
[_box setFrame:CGRectMake(_box.frame.origin.x,
MAX(0, pointInWindow.y - distanceBetweenViewAndBox - _box.frame.size.height),
_box.frame.size.width,
_box.frame.size.height)];
}
/// Prio 3: Place the box below the element.
else if (heightDown > heightUp && heightDown > _box.frame.size.height + 2*distanceBetweenViewAndBox) {
[_box setFrame:CGRectMake(_box.frame.origin.x,
MIN(currentWindow.frame.size.height - _box.frame.size.height - 10, pointInWindow.y + view.frame.size.height + distanceBetweenViewAndBox),
_box.frame.size.width,
_box.frame.size.height)];
}
/// Prio 4: Place the box in the center of the screen.
else {
[_box setCenter:CGPointMake(_box.center.x, currentWindow.center.y)];
}
}
}
/*!
* Position the view (copy/snapshot) in the same place it is in the current screen.
* @param view The view that was snapshotted (in focus).
*/
- (void)positionView:(UIView *)view;
{
/// Position the view in focus.
CGPoint pointInWindow = [self convertPoint:view.bounds.origin fromView:view];
UIImageView *viewInFocusImageView = [[UIImageView alloc] initWithImage:[self imageWithView:view]];
viewInFocusImageView.frame = CGRectMake(pointInWindow.x, pointInWindow.y, view.frame.size.width, view.frame.size.height);
[self addSubview:viewInFocusImageView];
/// Bring the box to the front because it the most important view.
[self bringSubviewToFront:_box];
}
/*!
* Creates a UIImage from a UIView.
* @param view The UIView to take the screenshot.
* @return the image created from the UIView screenshot.
*/
- (UIImage *)imageWithView:(UIView *)view;
{
/// If it is iOS 7 or greater.
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
} else {
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
}
#pragma mark - UIView delegate
/*!
* Tells the receiver when one or more fingers are raised from a view or window.
* @param touches A set of UITouch instances that represent the touches for the ending phase of the event represented by event.
* @param event An object representing the event to which the touches belong.
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
/// Removes itself from the superview.
[self removeFromSuperview];
/// If there is a completion block, execute it.
if (_completionBlock) {
_completionBlock();
}
}
/*!
* Lays out subviews. Make the layout for subviews, if needed it.
*/
- (void)layoutSubviews;
{
[super layoutSubviews];
/// If there is a view to focus on, change the layout of the box.
if (_viewInFocus) {
/// Positioning the box relative to the view in focus.
[self positionBox:_viewInFocus];
/// Positioning the view in the right place.
[self positionView:_viewInFocus];
}
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[self setFrame:currentWindow.frame];
/// Adjust subviews.
[self layoutIfNeeded];
}
@end