1
+
2
+ //
3
+ // LineLayout.m
4
+ // TestCollection
5
+ //
6
+ // Created by Eric on 15/5/14.
7
+ // Copyright (c) 2015年 eric. All rights reserved.
8
+ //
9
+ #import " CardLayout.h"
10
+
11
+
12
+
13
+ @interface CardLayout ()
14
+
15
+ @property (nonatomic ,strong ) UICollectionViewLayoutAttributes* attributes;
16
+
17
+
18
+ @end
19
+ #define RIDO_TO_5S [[UIScreen mainScreen ] bounds ].size.width / 320.0
20
+ #define ITEM_SIZE_HEIGHT 267.0
21
+ #define ITEM_SIZE_WIDTH 205.0
22
+ @implementation CardLayout
23
+
24
+ #define ACTIVE_DISTANCE 100
25
+ #define ZOOM_FACTOR 0.4
26
+
27
+
28
+ -(id )init
29
+ {
30
+ self = [super init ];
31
+ if (self) {
32
+ self.itemSize = CGSizeMake (ITEM_SIZE_WIDTH*RIDO_TO_5S, ITEM_SIZE_HEIGHT*RIDO_TO_5S);
33
+ self.scrollDirection = UICollectionViewScrollDirectionVertical;
34
+ self.sectionInset = UIEdgeInsetsMake (130 *RIDO_TO_5S, 0.0 , 180 *RIDO_TO_5S, 0.0 );
35
+ self.LineSpace = -237 ;
36
+ self.Zoom = ZOOM_FACTOR;
37
+ self.minimumLineSpacing = self.LineSpace *RIDO_TO_5S ;
38
+
39
+ }
40
+ return self;
41
+ }
42
+
43
+
44
+
45
+ - (BOOL )shouldInvalidateLayoutForBoundsChange : (CGRect)oldBounds
46
+ {
47
+ return YES ;
48
+ }
49
+
50
+
51
+
52
+ -(NSArray *)layoutAttributesForElementsInRect : (CGRect)rect
53
+ {
54
+ NSArray * array = [super layoutAttributesForElementsInRect: rect];
55
+ CGRect visibleRect;
56
+ visibleRect.origin = self.collectionView .contentOffset ;
57
+ visibleRect.size = self.collectionView .bounds .size ;
58
+
59
+ for (UICollectionViewLayoutAttributes* attributes in array) {
60
+
61
+ attributes.zIndex = attributes.indexPath .row ;// z轴排序
62
+
63
+ if (CGRectIntersectsRect (attributes.frame , rect)) {
64
+
65
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
66
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
67
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
68
+
69
+
70
+ if (ABS (distance) < 30 ) {
71
+ if (attributes != nil && self.attributes .indexPath .row !=attributes.indexPath .row ) {
72
+ self.attributes = [attributes copy ];
73
+ if ([_delegate respondsToSelector: @selector (CardLayout:didscrollToItematRow: )]) {
74
+ [_delegate CardLayout: self didscrollToItematRow: (int )self .attributes.indexPath.row];
75
+ }
76
+
77
+ }
78
+ // NSLog(@"%ld",(long)self.attributes.indexPath.row);
79
+
80
+
81
+
82
+ for ( UICollectionViewLayoutAttributes* attributes in array) {
83
+ if (attributes.indexPath .row >self.attributes .indexPath .row +1 ||attributes.indexPath .row <self.attributes .indexPath .row -7 ) {
84
+
85
+ attributes.transform3D = CATransform3DMakeScale (zoom, zoom, 1.0 );
86
+ attributes.hidden = YES ;
87
+
88
+
89
+
90
+
91
+
92
+ }else if (attributes.indexPath .row <self.attributes .indexPath .row ){
93
+
94
+ int alphaindex ;
95
+ alphaindex = (int )(self.attributes .indexPath .row -attributes.indexPath .row );
96
+
97
+
98
+ attributes.alpha =(7 -alphaindex)*0.2 ;
99
+
100
+
101
+ // 修改间距代码
102
+ // attributes.transform3D = CATransform3DIdentity;
103
+ // CGRect frame = attributes.frame;
104
+ //
105
+ // switch (alphaindex) {
106
+ // case 1:
107
+ // frame.origin.y -= 10;
108
+ // break;
109
+ // case 2:
110
+ // frame.origin.y -= 3;
111
+ // break;
112
+ // case 3:
113
+ // break;
114
+ // case 4:
115
+ // frame.origin.y += 10;
116
+ // break;
117
+ // case 5:
118
+ // frame.origin.y += 20;
119
+ // break;
120
+ // case 6:
121
+ // frame.origin.y += 30;
122
+ // break;
123
+ //
124
+ //
125
+ // default:
126
+ //
127
+ // break;
128
+ // }
129
+ //
130
+ // attributes.frame = frame;
131
+
132
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
133
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
134
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
135
+
136
+ attributes.transform3D = CATransform3DMakeScale (zoom, zoom, 1.0 );
137
+
138
+
139
+ }else if (attributes.indexPath .row ==self.attributes .indexPath .row +1 ){
140
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
141
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
142
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
143
+
144
+ int z =(int )(zoom*1000 );
145
+ // NSLog(@"%d",z);
146
+ attributes.transform3D =CATransform3DIdentity ;
147
+ CGRect frame;
148
+ frame = attributes.frame ;
149
+ frame.origin .y +=1.8 *abs (1400 -z)*RIDO_TO_5S;
150
+ attributes.frame = frame;
151
+ attributes.transform3D = CATransform3DMakeScale (1.4 , 1.4 , 1.0 ) ;
152
+
153
+
154
+ }
155
+ else if (self.attributes .indexPath .row == attributes.indexPath .row ){
156
+ attributes.transform3D =CATransform3DIdentity ;
157
+
158
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
159
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
160
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
161
+
162
+ attributes.transform3D = CATransform3DMakeScale (1.4 , 1.4 , 1.0 );
163
+
164
+ }
165
+
166
+ }
167
+ }
168
+
169
+ }
170
+ }
171
+
172
+ for ( UICollectionViewLayoutAttributes* attributes in array) {
173
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
174
+
175
+ if (attributes.indexPath .row >self.attributes .indexPath .row +1 ||attributes.indexPath .row <self.attributes .indexPath .row -7 ) {
176
+ // CGFloat distance = CGRectGetMidY(visibleRect) - attributes.center.y;
177
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
178
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
179
+ attributes.transform3D = CATransform3DMakeScale (zoom, zoom, 1.0 );
180
+ attributes.hidden = YES ;
181
+
182
+
183
+
184
+
185
+
186
+ }else if (attributes.indexPath .row <self.attributes .indexPath .row ){
187
+
188
+ int alphaindex ;
189
+ alphaindex = (int )(self.attributes .indexPath .row -attributes.indexPath .row );
190
+
191
+
192
+ attributes.alpha =(7 -alphaindex)*0.2 ;
193
+
194
+
195
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
196
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
197
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
198
+
199
+ attributes.transform3D = CATransform3DMakeScale (zoom, zoom, 1.0 );
200
+
201
+
202
+ }else if (attributes.indexPath .row ==self.attributes .indexPath .row +1 ){
203
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
204
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
205
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
206
+
207
+ int z =(int )(zoom*1000 );
208
+ // NSLog(@"%d",z);
209
+ attributes.transform3D =CATransform3DIdentity ;
210
+ CGRect frame;
211
+ frame = attributes.frame ;
212
+ frame.origin .y +=0.2 *abs (1400 -z)*RIDO_TO_5S;
213
+ attributes.frame = frame;
214
+ attributes.transform3D =CATransform3DMakeScale (1.4 , 1.4 , 1.0 ) ;
215
+ }
216
+ else if (self.attributes .indexPath .row == attributes.indexPath .row ){
217
+ attributes.transform3D =CATransform3DIdentity ;
218
+
219
+ CGFloat distance = CGRectGetMidY (visibleRect) - attributes.center .y ;
220
+ CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
221
+ CGFloat zoom = 1 + self.Zoom *(1 - ABS (normalizedDistance));
222
+
223
+ attributes.transform3D = CATransform3DMakeScale (1.4 , 1.4 , 1.0 );
224
+
225
+
226
+
227
+ }
228
+
229
+ }
230
+
231
+ return array;
232
+ }
233
+
234
+
235
+ - (CGPoint)targetContentOffsetForProposedContentOffset : (CGPoint)proposedContentOffset withScrollingVelocity : (CGPoint)velocity
236
+ {
237
+ CGFloat offsetAdjustment = MAXFLOAT;
238
+ CGFloat horizontalCenter = proposedContentOffset.y + (CGRectGetHeight (self.collectionView .bounds ) / 2.0 );
239
+
240
+ CGRect targetRect = CGRectMake (0.0 , proposedContentOffset.y , self.collectionView .bounds .size .width , self.collectionView .bounds .size .height );
241
+ NSArray * array = [super layoutAttributesForElementsInRect: targetRect];
242
+
243
+ for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
244
+
245
+ CGFloat itemHorizontalCenter = layoutAttributes.center .y ;
246
+ if (ABS (itemHorizontalCenter - horizontalCenter) < ABS (offsetAdjustment)) {
247
+ offsetAdjustment = itemHorizontalCenter - horizontalCenter;
248
+
249
+ // 调整
250
+
251
+
252
+ }
253
+ }
254
+
255
+ return CGPointMake (proposedContentOffset.x , proposedContentOffset.y + offsetAdjustment );
256
+ }
257
+
258
+ @end
0 commit comments