/
MOOCreateView.m
262 lines (199 loc) · 8.21 KB
/
MOOCreateView.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
//
// MOORefreshView.m
// MOOPullGesture
//
// Created by Peyton Randolph
//
#import "MOOCreateView.h"
#import <QuartzCore/QuartzCore.h>
#import "ARCHelper.h"
@interface MOOCreateView ()
@property (nonatomic, strong) MOOGradientView *gradientView;
@property (nonatomic, strong) UIView *rotationView;
// Notification handling
- (void)handleContentOffsetChangedNotification:(NSNotification *)notification;
@end
@implementation MOOCreateView
@synthesize delegate = _delegate;
@synthesize configurationBlock = _configurationBlock;
@synthesize cell = _cell;
@synthesize gradientView = _gradientView;
@synthesize rotationView = _rotationView;
- (id)init;
{
if (!(self = [self initWithCell:[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]]))
return nil;
// UITableViewCell has, by default, a transparent background. Set to white.
self.cell.backgroundColor = [UIColor whiteColor];
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shouldRasterize = YES;
return self;
}
- (id)initWithCell:(UITableViewCell *)cell;
{
if (!(self = [super initWithFrame:CGRectZero]))
return nil;
// Configure view
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor blackColor];
self.clipsToBounds = YES;
// Create transform layer
self.rotationView = [[UIView alloc] initWithFrame:CGRectZero];
// Allow antialiasing
self.rotationView.layer.borderColor = self.backgroundColor.CGColor;
self.rotationView.layer.borderWidth = 3.0f;
self.rotationView.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.rotationView.layer.shouldRasterize = YES;
// Configure cell
self.cell = cell;
// Create gradient view
self.gradientView = [[MOOGradientView alloc] initWithFrame:CGRectZero];
self.gradientView.layer.anchorPoint = CGPointZero;
// Configure shadow gradient. If the effect is too strong, you can access the gradient view through the gradientView property and change the gradient colors and locations.
((CAGradientLayer *)self.gradientView.layer).colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0f alpha:0.4f].CGColor, (id)[UIColor colorWithWhite:0.0f alpha:0.8f].CGColor, nil];
((CAGradientLayer *)self.gradientView.layer).locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil];
// Register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleContentOffsetChangedNotification:) name:MOONotificationContentOffsetChanged object:nil];
return self;
}
- (id)initWithFrame:(CGRect)frame;
{
NSString *reason = [NSString stringWithFormat:@"Sent %@ to %@. Use %@ or %@ instead.", NSStringFromSelector(_cmd), NSStringFromClass([self class]), NSStringFromSelector(@selector(init)), NSStringFromSelector(@selector(initWithCellClass:style:))];
@throw([NSException exceptionWithName:NSInternalInconsistencyException reason:reason userInfo:nil]);
}
- (void)dealloc;
{
self.delegate = nil;
self.cell = nil;
self.configurationBlock = nil;
self.gradientView = nil;
self.rotationView = nil;
AH_SUPER_DEALLOC;
}
#pragma mark - Subview methods
- (void)layoutSubviews;
{
// Expand cell, gradientView, and rotationView to fit createView
self.cell.layer.bounds = self.bounds;
self.gradientView.layer.bounds = self.bounds;
self.rotationView.layer.bounds = CGRectInset(self.bounds, -self.rotationView.layer.borderWidth, -self.rotationView.layer.borderWidth);
// Set rotation view anchorPoint to the max Y, accounting for border width
self.rotationView.layer.anchorPoint = CGPointMake(0.5f, 1.0f - self.rotationView.layer.borderWidth / CGRectGetHeight(self.rotationView.layer.bounds));
// Position cell in the center of rotation view
CGPoint rotationCenter = CGPointMake(CGRectGetMidX(self.rotationView.layer.bounds), CGRectGetMidY(self.rotationView.layer.bounds));
self.cell.layer.position = rotationCenter;
// Position gradientView in the top left
self.gradientView.layer.position = CGPointZero;
}
- (CGSize)sizeThatFits:(CGSize)size;
{
return size;
}
#pragma mark - MOOTriggerView methods
- (void)positionInScrollView:(UIScrollView *)scrollView;
{
// Size create view
CGFloat height = ([scrollView isKindOfClass:[UITableView class]]) ? height = ((UITableView *)scrollView).rowHeight : 44.0f;
CGSize triggerViewSize = [self sizeThatFits:CGSizeMake(CGRectGetWidth(scrollView.bounds), height)];
// Position create view
CGPoint triggerViewOrigin = CGPointMake(0.0, -triggerViewSize.height);
CGRect triggerViewFrame = CGRectZero;
triggerViewFrame.size = triggerViewSize;
triggerViewFrame.origin = triggerViewOrigin;
self.frame = triggerViewFrame;
}
- (void)transitionToPullState:(MOOPullState)pullState;
{
if (self.configurationBlock)
self.configurationBlock(self, self.cell, pullState);
else if ([self.delegate respondsToSelector:@selector(createView:configureCell:forState:)])
[self.delegate createView:self configureCell:self.cell forState:pullState];
switch (pullState)
{
case MOOPullIdle:
self.hidden = NO;
break;
case MOOPullTriggered:
self.hidden = YES;
break;
case MOOPullActive:
default:
break;
}
}
#pragma mark - Notification handling
- (void)handleContentOffsetChangedNotification:(NSNotification *)notification;
{
/*
* Layer folding effect
*/
// Grab content offset
CGPoint contentOffet = [[notification.userInfo objectForKey:MOOKeyContentOffset] CGPointValue];
// Calculate transition progress
CGFloat progress = MIN(-contentOffet.y / CGRectGetHeight(self.bounds), 1.0f);
//
CGFloat angle = acosf(progress);
if (isnan(angle))
angle = 0.0f;
CATransform3D transform = CATransform3DMakeRotation(angle, 1.0f, 0.0f, 0.0f);
// Perspective transform. Gradually decreases based on progress
if (angle > 0.0f)
transform.m24 = -1.f / 300.f + 1.f / 300.f * progress;
self.rotationView.layer.transform = transform;
// Position at bottom of create view
CGFloat positionY = CGRectGetHeight(self.layer.bounds);
// 1px adjustment for table views with a 1px cell separator
if ([self.superview isKindOfClass:[UITableView class]])
if (((UITableView *)self.superview).separatorStyle == UITableViewCellSeparatorStyleSingleLine)
positionY -= 1.0f - progress;
self.rotationView.layer.position = CGPointMake(CGRectGetMidX(self.layer.bounds), positionY);
// Set opacity to mimic shadows
self.gradientView.layer.opacity = MAX(1.0f - progress, 0.0f);
}
#pragma mark - Getters and setters
- (void)setCell:(UITableViewCell *)cell;
{
if (cell == self.cell)
return;
[self.cell removeFromSuperview];
_cell = cell;
// Insert below gradientView if it exists
if ([self.rotationView.subviews containsObject:self.gradientView])
[self.rotationView insertSubview:cell belowSubview:self.gradientView];
else
[self.rotationView addSubview:cell];
[self setNeedsLayout];
}
- (void)setGradientView:(MOOGradientView *)gradientView;
{
if (gradientView == self.gradientView)
return;
[self.gradientView removeFromSuperview];
_gradientView = gradientView;
[self.rotationView addSubview:self.gradientView];
[self setNeedsLayout];
}
- (void)setRotationView:(UIView *)rotationView;
{
if (rotationView == self.rotationView)
return;
// Moves rotationView subviews to new rotationView
for (UIView *subview in self.rotationView.subviews)
{
[rotationView addSubview:subview];
}
// Swap rotationViews
[self.rotationView removeFromSuperview];
_rotationView = rotationView;
// Configure new rotationView
[self addSubview:rotationView];
rotationView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self setNeedsLayout];
}
@end
@implementation MOOGradientView
+ (Class)layerClass;
{
return [CAGradientLayer class];
}
@end