-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathRGCardViewLayout.m
179 lines (149 loc) · 4.99 KB
/
RGCardViewLayout.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
//
// RGCardViewLayout.m
// RGCardViewLayout
//
// Created by ROBERA GELETA on 1/23/15.
// Copyright (c) 2015 ROBERA GELETA. All rights reserved.
//
#import "RGCardViewLayout.h"
@implementation RGCardViewLayout
{
CGFloat previousOffset;
NSIndexPath *mainIndexPath;
NSIndexPath *movingInIndexPath;
CGFloat difference;
}
- (void)prepareLayout
{
[self setupLayout];
[super prepareLayout];
}
- (void)setupLayout
{
CGFloat inset = self.collectionView.bounds.size.width * (6/64.0f);
inset = floor(inset);
self.itemSize = CGSizeMake(self.collectionView.bounds.size.width - (2 *inset), self.collectionView.bounds.size.height * 3/4);
self.sectionInset = UIEdgeInsetsMake(0,inset, 0,inset);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];
[self applyTransformToLayoutAttributes:attributes];
return attributes;
}
// indicate that we want to redraw as we scroll
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attributesSuper = [super layoutAttributesForElementsInRect:rect];
NSArray *attributes = [[NSArray alloc] initWithArray:attributesSuper copyItems:YES];
NSArray *cellIndices = [self.collectionView indexPathsForVisibleItems];
if(cellIndices.count == 0 )
{
return attributes;
}
else if (cellIndices.count == 1)
{
mainIndexPath = cellIndices.firstObject;
movingInIndexPath = nil;
}
else if(cellIndices.count > 1)
{
NSIndexPath *firstIndexPath = cellIndices.firstObject;
if(firstIndexPath == mainIndexPath)
{
movingInIndexPath = cellIndices[1];
}
else
{
movingInIndexPath = cellIndices.firstObject;
mainIndexPath = cellIndices[1];
}
}
difference = self.collectionView.contentOffset.x - previousOffset;
previousOffset = self.collectionView.contentOffset.x;
for (UICollectionViewLayoutAttributes *attribute in attributes)
{
[self applyTransformToLayoutAttributes:attribute];
}
return attributes;
}
- (void)applyTransformToLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute
{
if(attribute.indexPath.section == mainIndexPath.section)
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:mainIndexPath];
attribute.transform3D = [self transformFromView:cell];
}
else if (attribute.indexPath.section == movingInIndexPath.section)
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:movingInIndexPath];
attribute.transform3D = [self transformFromView:cell];
}
}
#pragma mark - Logica
- (CGFloat)baseOffsetForView:(UIView *)view
{
UICollectionViewCell *cell = (UICollectionViewCell *)view;
CGFloat offset = ([self.collectionView indexPathForCell:cell].section) * self.collectionView.bounds.size.width;
return offset;
}
- (CGFloat)heightOffsetForView:(UIView *)view
{
CGFloat height;
CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
CGFloat currentOffset = self.collectionView.contentOffset.x;
CGFloat scrollViewWidth = self.collectionView.bounds.size.width;
//TODO:make this constant a certain proportion of the collection view
height = 120 * (currentOffset - baseOffsetForCurrentView)/scrollViewWidth;
if(height < 0 )
{
height = - 1 * height;
}
return height;
}
- (CGFloat)angleForView:(UIView *)view
{
CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
CGFloat currentOffset = self.collectionView.contentOffset.x;
CGFloat scrollViewWidth = self.collectionView.bounds.size.width;
CGFloat angle = (currentOffset - baseOffsetForCurrentView)/scrollViewWidth;
return angle;
}
- (BOOL)xAxisForView:(UIView *)view
{
CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
CGFloat currentOffset = self.collectionView.contentOffset.x;
CGFloat offset = (currentOffset - baseOffsetForCurrentView);
if(offset >= 0)
{
return YES;
}
return NO;
}
#pragma mark - Transform Related Calculation
- (CATransform3D)transformFromView:(UIView *)view
{
CGFloat angle = [self angleForView:view];
CGFloat height = [self heightOffsetForView:view];
BOOL xAxis = [self xAxisForView:view];
return [self transformfromAngle:angle height:height xAxis:xAxis];
}
- (CATransform3D)transformfromAngle:(CGFloat)angle height:(CGFloat)height xAxis:(BOOL)axis
{
CATransform3D t = CATransform3DIdentity;
t.m34 = 1.0/-500;
if (axis)
{
t = CATransform3DRotate(t,angle, 1, 1, 0);
}
else
{
t = CATransform3DRotate(t,angle, -1, 1, 0);
}
return t;
}
@end