-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYALContextMenuTableView.m
executable file
·360 lines (283 loc) · 11 KB
/
YALContextMenuTableView.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// YALTableView.m
// YALMenuAnimation
//
// Created by Maksym Lazebnyi on 1/12/15.
// Copyright (c) 2015 Yalantis. All rights reserved.
//
#import "YALContextMenuTableView.h"
#import "UIView+YALConstraints.h"
#import "YALContextMenuCell.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
static CGFloat const defaultDuration = 0.05;
static CGPoint const defaultViewAnchorPoint = {0.5f, 0.5f};
typedef void(^completionBlock)(BOOL completed);
typedef NS_ENUM(NSUInteger, Direction) {
right,
top,
bottom
};
typedef NS_ENUM(NSUInteger, AnimatingState) {
Hiding = -1,
Stable = 0,
Showing = 1
};
@interface YALContextMenuTableView ()
@property (nonatomic) __block NSInteger animatingIndex;
@property (nonatomic, strong) NSMutableArray *topCells;
@property (nonatomic, strong) NSMutableArray *bottomCells;
@property (nonatomic, strong) UITableViewCell<YALContextMenuCell> *selectedCell;
@property (nonatomic, strong) NSIndexPath *dismissalIndexpath;
@property (nonatomic) AnimatingState animatingState;
@end
@implementation YALContextMenuTableView
#pragma mark - Init
- (instancetype)initWithTableViewDelegateDataSource:(id<UITableViewDelegate, UITableViewDataSource>)delegateDataSource {
self = [self init];
if (self) {
self.delegate = delegateDataSource;
self.dataSource = delegateDataSource;
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
self.animatingState = Stable;
self.animationDuration = defaultDuration;
self.animatingIndex = 0;
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
self.separatorColor = [UIColor colorWithRed:181.0/255.0 green:181.0/255.0 blue:181.0/255.0 alpha:0];
self.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; //Zero rect footer to clear empty rows UITableView draws
}
return self;
}
#pragma mark - Show / Dismiss
- (void)showInView:(UIView *)superview withEdgeInsets:(UIEdgeInsets)edgeInsets animated:(BOOL)animated {
if (self.animatingState!=Stable) {
return;
}
for (UITableViewCell<YALContextMenuCell> *aCell in [self visibleCells]) {
aCell.contentView.hidden = YES;
}
self.dismissalIndexpath = nil;
[superview addSubViewiew:self withSidesConstrainsInsets:edgeInsets];
if (animated) {
self.animatingState = Showing;
self.alpha = 0;
[self setUserInteractionEnabled:NO];
[UIView animateWithDuration:self.animationDuration animations:^{
self.alpha = 1;
} completion:^(BOOL finished) {
[self show:YES visibleCellsAnimated:YES];
[self setUserInteractionEnabled:YES];
}];
} else {
[self show:YES visibleCellsAnimated:NO];
}
}
- (void)dismisWithIndexPath:(NSIndexPath *)indexPath {
if (self.animatingState!=Stable) {
return;
}
if (!indexPath) {
[self removeFromSuperview];
return;
}
self.dismissalIndexpath = indexPath;
self.animatingState = Hiding;
[self setUserInteractionEnabled:NO];
NSArray *visibleCells = [self visibleCells];
self.topCells = [NSMutableArray array];
self.bottomCells = [NSMutableArray array];
for (UITableViewCell<YALContextMenuCell> *visibleCell in visibleCells) {
if ([self indexPathForCell:visibleCell].row<indexPath.row) {
[self.topCells addObject: visibleCell];
} else if ([self indexPathForCell:visibleCell].row>indexPath.row) {
[self.bottomCells addObject: visibleCell];
} else {
self.selectedCell = visibleCell;
}
}
[self dismissTopCells];
[self dismissBottomCells];
}
- (void)updateAlongsideRotation {
if (self.animatingState == Hiding) {
if ([self.yalDelegate respondsToSelector:@selector(contextMenuTableView:didDismissWithIndexPath:)]) {
[self.yalDelegate contextMenuTableView:self didDismissWithIndexPath:[self indexPathForCell:self.selectedCell]];
}
[self removeFromSuperview];
} else if (self.animatingState == Showing) {
[self show:YES visibleCellsAnimated:NO];
}
self.animatingState = Stable;
}
#pragma mark - Private
- (void)show:(BOOL)show visibleCellsAnimated:(BOOL)animated {
NSArray *visibleCells = [self visibleCells];
if (visibleCells.count == 0 || self.animatingIndex == visibleCells.count) {
self.animatingIndex = 0;
[self setUserInteractionEnabled:YES];
[self reloadData];
self.animatingState = Stable;
return;
}
UITableViewCell<YALContextMenuCell> *visibleCell = [visibleCells objectAtIndex:self.animatingIndex];
if (visibleCell) {
[self prepareCellForShowAnimation:visibleCell];
[visibleCell contentView].hidden = NO;
Direction direction = ([visibleCells indexOfObject:visibleCell] == 0) ? right : top;
[self show:show cell:visibleCell animated:animated direction:direction clockwise:NO completion:^(BOOL completed) {
if (completed) {
self.animatingIndex++;
[self show:show visibleCellsAnimated:animated];
}
}];
}
}
- (void)dismissBottomCells {
if (self.bottomCells.count) {
UITableViewCell<YALContextMenuCell> *hidingCell = [self.bottomCells lastObject];
[self show:NO cell:hidingCell animated:YES direction:top clockwise:NO completion:^(BOOL completed) {
if (completed) {
[self.bottomCells removeLastObject];
[self dismissBottomCells];
[self shouldDismissSelf];
}
}];
}
}
- (void)dismissTopCells {
if (self.topCells.count) {
UITableViewCell<YALContextMenuCell> *hidingCell = [self.topCells firstObject];
[self show:NO cell:hidingCell animated:YES direction:bottom clockwise:YES completion:^(BOOL completed) {
if (completed) {
[self.topCells removeObjectAtIndex:0];
[self dismissTopCells];
[self shouldDismissSelf];
}
}];
}
}
- (void)dismissSelf {
[self show:NO cell:self.selectedCell animated:YES direction:right clockwise:NO completion:^(BOOL completed) {
[self removeFromSuperview];
if ([self.yalDelegate respondsToSelector:@selector(contextMenuTableView:didDismissWithIndexPath:)]) {
[self.yalDelegate contextMenuTableView:self didDismissWithIndexPath:[self indexPathForCell:self.selectedCell]];
}
self.animatingState = Stable;
}];
}
- (void)shouldDismissSelf {
if (self.bottomCells.count == 0 && self.topCells.count == 0) {
[self dismissSelf];
}
}
#pragma mark - Animate Cells
- (void)prepareCellForShowAnimation:(UITableViewCell<YALContextMenuCell> *)cell {
[self resetAnimatedIconForCell:cell];
Direction direction = ([self indexPathForCell:cell].row == 0) ? right : top;
[self show:NO cell:cell animated:NO direction:direction clockwise:NO completion:nil];
}
/*!
@abstract
Call this method for cell view to be hidden or shown
@param show - YES to be shown, NO to be hidden
@param animated - Yes or NO for animation
@param animation - Duration of animation with seconds
@param direction - Define the side for icon to animate
@param clockwise - Set YES to rotate clockwise
@param completed - block called on the completion of the animation
*/
- (void)show:(BOOL)show
cell:(UITableViewCell<YALContextMenuCell> *)cell
animated:(BOOL)animated
direction:(Direction)direction
clockwise:(BOOL)clockwise
completion:(completionBlock)completed {
UIView *icon = [cell animatedIcon];
UIView *content = [cell animatedContent];
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0/200; //Add the perspective
CGFloat rotation = 90;
if (clockwise) {
rotation = -rotation;
}
if (show) {
rotation = 0;
}
switch (direction) {
case right: {
[self setAnchorPoint:CGPointMake(1, 0.5) forView:icon];
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DEGREES_TO_RADIANS(rotation), 0.0f, 1.0f, 0.0f);
break;
}
case top: {
[self setAnchorPoint:CGPointMake(0.5, 0.0) forView:icon];
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DEGREES_TO_RADIANS(rotation), 1.0f, 0.0f, 0.0f);
break;
}
case bottom: {
[self setAnchorPoint:CGPointMake(0.5, 1) forView:icon];
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DEGREES_TO_RADIANS(rotation), 1.0f, 0.0f, 0.0f);
break;
}
default:
break;
}
if (animated) {
[UIView animateWithDuration:self.animationDuration animations:^{
icon.layer.transform = rotationAndPerspectiveTransform;
content.alpha = show;
} completion:^(BOOL finished) {
if (completed) {
completed(finished);
}
}];
} else {
icon.layer.transform = rotationAndPerspectiveTransform;
content.alpha = show;
if (completed) {
completed(YES);
}
}
}
#pragma mark - Overriden
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
UITableViewCell<YALContextMenuCell> *cell = [super dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
[self resetAnimatedIconForCell:cell];
if (cell) {
if (self.animatingState==Showing) {
[cell contentView].hidden = YES;
}else if (self.animatingState==Stable)
{
[cell contentView].hidden = NO;
[cell animatedContent].alpha = 1;
}
}
return cell;
}
#pragma mark - Utils
- (void)resetAnimatedIconForCell:(UITableViewCell<YALContextMenuCell> *) cell{
UIView *icon = [cell animatedIcon];
icon.layer.anchorPoint = defaultViewAnchorPoint;
icon.layer.transform = CATransform3DIdentity;
[icon layoutIfNeeded];
}
- (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view {
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x,
view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x,
view.bounds.size.height * view.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
CGPoint position = view.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}
@end