-
Notifications
You must be signed in to change notification settings - Fork 43
/
ADLivelyCollectionView.m
203 lines (174 loc) · 7.28 KB
/
ADLivelyCollectionView.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
//
// ADLivelyCollectionView.m
// ADLivelyCollectionView
//
// Created by Romain Goyet on 18/04/12.
// Copyright (c) 2012 Applidium. All rights reserved.
//
#import "ADLivelyCollectionView.h"
#import <QuartzCore/QuartzCore.h>
NSTimeInterval ADLivelyDefaultDuration = 0.2;
CGFloat CGFloatSign(CGFloat value) {
if (value < 0) {
return -1.0f;
}
return 1.0f;
}
ADLivelyTransform ADLivelyTransformCurl = ^(CALayer * layer, float speed){
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -500;
transform = CATransform3DTranslate(transform, -layer.bounds.size.width/2.0f, 0.0f, 0.0f);
transform = CATransform3DRotate(transform, M_PI/2, 0.0f, 1.0f, 0.0f);
layer.transform = CATransform3DTranslate(transform, layer.bounds.size.width/2.0f, 0.0f, 0.0f);
return ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformFade = ^(CALayer * layer, float speed){
if (speed != 0.0f) { // Don't animate the initial state
layer.opacity = 1.0f - fabs(speed);
}
return 2 * ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformFan = ^(CALayer * layer, float speed){
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -layer.bounds.size.width/2.0f, 0.0f, 0.0f);
transform = CATransform3DRotate(transform, -M_PI/2 * speed, 0.0f, 0.0f, 1.0f);
layer.transform = CATransform3DTranslate(transform, layer.bounds.size.width/2.0f, 0.0f, 0.0f);
layer.opacity = 1.0f - fabs(speed);
return ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformFlip = ^(CALayer * layer, float speed){
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0.0f, CGFloatSign(speed) * layer.bounds.size.height/2.0f, 0.0f);
transform = CATransform3DRotate(transform, CGFloatSign(speed) * M_PI/2, 1.0f, 0.0f, 0.0f);
layer.transform = CATransform3DTranslate(transform, 0.0f, -CGFloatSign(speed) * layer.bounds.size.height/2.0f, 0.0f);
layer.opacity = 1.0f - fabs(speed);
return 2 * ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformHelix = ^(CALayer * layer, float speed){
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0.0f, CGFloatSign(speed) * layer.bounds.size.height/2.0f, 0.0f);
transform = CATransform3DRotate(transform, M_PI, 0.0f, 1.0f, 0.0f);
layer.transform = CATransform3DTranslate(transform, 0.0f, -CGFloatSign(speed) * layer.bounds.size.height/2.0f, 0.0f);
layer.opacity = 1.0f - 0.2*fabs(speed);
return 2 * ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformTilt = ^(CALayer * layer, float speed){
if (speed != 0.0f) { // Don't animate the initial state
layer.transform = CATransform3DMakeScale(0.8f, 0.8f, 0.8f);
layer.opacity = 1.0f - fabs(speed);
}
return 2 * ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformWave = ^(CALayer * layer, float speed){
if (speed != 0.0f) { // Don't animate the initial state
layer.transform = CATransform3DMakeTranslation(-layer.bounds.size.width/2.0f, 0.0f, 0.0f);
}
return ADLivelyDefaultDuration;
};
ADLivelyTransform ADLivelyTransformGrow = ^(CALayer * layer, float speed){
if (speed != 0.0f) { // Don't animate the initial state
layer.transform = CATransform3DMakeScale(0.0, 0.0, 1.0);
}
return ADLivelyDefaultDuration;
};
@implementation ADLivelyCollectionView
#pragma mark - NSObject
- (void)dealloc {
Block_release(_transformBlock);
[super dealloc];
}
#pragma mark - UIView
+ (Class)layerClass {
// This lets us rotate cells in the collectionview's 3D space
return [CATransformLayer class];
}
#pragma mark - UICollectionView
- (void)setDelegate:(id<UICollectionViewDelegate>)delegate {
// The order here is important, as there seem to be some observing done on setDelegate:
if (delegate == self) {
_preLivelyDelegate = nil;
} else {
_preLivelyDelegate = delegate;
}
[super setDelegate:self];
}
- (id<UICollectionViewDelegate>)delegate {
return _preLivelyDelegate;
}
- (void)setDataSource:(id<UICollectionViewDataSource>)dataSource {
// The order here is important, as there seem to be some observing done on setDelegate:
if (dataSource == self) {
_preLivelyDataSource = nil;
} else {
_preLivelyDataSource = dataSource;
}
[super setDataSource:self];
}
- (id<UICollectionViewDataSource>)dataSource {
return _preLivelyDataSource;
}
- (BOOL)respondsToSelector:(SEL)aSelector {
return [super respondsToSelector:aSelector] || [_preLivelyDelegate respondsToSelector:aSelector] || [_preLivelyDataSource respondsToSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if ([_preLivelyDelegate respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:_preLivelyDelegate];
} else if ([_preLivelyDataSource respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:_preLivelyDataSource];
} else {
[super forwardInvocation:anInvocation];
}
}
#pragma mark - ADLivelyCollectionView
- (CGPoint)scrollSpeed {
return CGPointMake(_lastScrollPosition.x - _currentScrollPosition.x,
_lastScrollPosition.y - _currentScrollPosition.y);
}
- (void)setInitialCellTransformBlock:(ADLivelyTransform)block {
CATransform3D transform = CATransform3DIdentity;
if (block != nil) {
transform.m34 = -1.0/self.bounds.size.width;
}
self.layer.transform = transform;
if (block != _transformBlock) {
Block_release(_transformBlock);
_transformBlock = Block_copy(block);
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([_preLivelyDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[_preLivelyDelegate scrollViewDidScroll:scrollView];
}
_lastScrollPosition = _currentScrollPosition;
_currentScrollPosition = [scrollView contentOffset];
}
#pragma mark - UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell * cell = nil;
if ([_preLivelyDataSource respondsToSelector:@selector(collectionView:cellForItemAtIndexPath:)]) {
cell = [_preLivelyDataSource collectionView:collectionView cellForItemAtIndexPath:indexPath];
}
float speed = self.scrollSpeed.y;
float normalizedSpeed = MAX(-1.0f, MIN(1.0f, speed/20.0f));
BOOL shouldAnimate = YES;
if (self.speedThreshold) {
shouldAnimate = fabsf(speed) < self.speedThreshold;
}
if (_transformBlock && shouldAnimate) {
NSTimeInterval animationDuration = _transformBlock(cell.layer, normalizedSpeed);
// The block-based equivalent doesn't play well with iOS 4
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
}
cell.layer.transform = CATransform3DIdentity;
cell.layer.opacity = 1.0f;
if (_transformBlock) {
[UIView commitAnimations];
}
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [_preLivelyDataSource collectionView:collectionView numberOfItemsInSection:section];
}
@end