-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCyrcleLayout.m
More file actions
325 lines (300 loc) · 13.1 KB
/
MyCyrcleLayout.m
File metadata and controls
325 lines (300 loc) · 13.1 KB
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
//
// MyCyrcleLayout.m
// CollectionViewPlayground
//
// Created by Oleksandr Kiporenko on 8/27/15.
// Copyright (c) 2015 QVC. All rights reserved.
//
#import "MyCyrcleLayout.h"
@interface MyCyrcleLayout()
//@property (nonatomic) CGRect prevBounds;
@property (nonatomic) CGFloat pageWidth;
@property NSInteger currentPage;
@property (nonatomic) CGFloat contentInsetLeftRight;
@end
@implementation MyCyrcleLayout
- (CGFloat)pageWidth
{
return self.itemSize.width + self.minimumLineSpacing;
}
-(void)prepareLayout
{
// Handle wrapping the collection view at the boundaries
if (self.scrollDirection == UICollectionViewScrollDirectionVertical)
{
if (self.infiniteScrollingEnabled)
{
if (self.collectionView.contentOffset.y <= 0.0f)
{
[self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x, [super collectionViewContentSize].height + self.minimumLineSpacing)];
}
else if (self.collectionView.contentOffset.y >= [super collectionViewContentSize].height + self.minimumLineSpacing)
{
[self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x, 0.0f)];
}
}
}
else
{
self.minimumInteritemSpacing = self.collectionView.bounds.size.height + self.itemSize.height;
if (self.infiniteScrollingEnabled)
{
CGFloat minXContentRange = 0.0f;
CGFloat maxXContentRange = [super collectionViewContentSize].width + self.minimumLineSpacing;
if (self.collectionView.contentOffset.x <= minXContentRange + self.contentInsetLeftRight)
{
// CGPoint newContentOffset = CGPointMake([super collectionViewContentSize].width + self.minimumLineSpacing + self.contentInsetLeftRight, self.collectionView.contentOffset.y);
//CGPoint newContentOffset = CGPointMake([super collectionViewContentSize].width + self.minimumLineSpacing, self.collectionView.contentOffset.y);
if (self.pagingStyle == MyCyrcleLayoutPagingStyleOff)
{
[self.collectionView setContentOffset:CGPointMake(maxXContentRange, self.collectionView.contentOffset.y)];
}
else
{
NSLog(@"(prepareLayout x<=0) old currentPage: %li", (long)self.currentPage);
self.currentPage += [self.collectionView numberOfItemsInSection:0];
NSLog(@"(prepareLayout x<=0) new currentPage: %li", (long)self.currentPage);
CGPoint newContentOffset = CGPointMake(self.currentPage * self.pageWidth, self.collectionView.contentOffset.y);
//self.currentPage = [self.collectionView numberOfItemsInSection:0] - self.currentPage;
[self.collectionView setContentOffset:newContentOffset animated:NO];
}
}
else if (self.collectionView.contentOffset.x >= maxXContentRange + self.contentInsetLeftRight)
{
if (self.pagingStyle == MyCyrcleLayoutPagingStyleOff)
{
[self.collectionView setContentOffset:CGPointMake(minXContentRange, self.collectionView.contentOffset.y)];
}
else
{
NSLog(@"(prepareLayout x>=width) old currentPage: %li", (long)self.currentPage);
self.currentPage = self.currentPage % [self.collectionView numberOfItemsInSection:0];
NSLog(@"(prepareLayout x>=width) new currentPage: %li", (long)self.currentPage);
CGPoint newContentOffset = CGPointMake(self.currentPage * self.pageWidth, self.collectionView.contentOffset.y);
//self.currentPage = 0;
[self.collectionView setContentOffset:newContentOffset animated:NO];
}
/*
Original:
if (self.collectionView.contentOffset.x <= 0.0f) {
[self.collectionView setContentOffset:CGPointMake([super collectionViewContentSize].width + self.minimumLineSpacing, self.collectionView.contentOffset.y)];
}
else if (self.collectionView.contentOffset.x >= [super collectionViewContentSize].width + self.minimumLineSpacing) {
[self.collectionView setContentOffset:CGPointMake(0.0f, self.collectionView.contentOffset.y)];
}
*/
}
}
}
[super prepareLayout];
}
- (CGSize)collectionViewContentSize
{
CGSize contentSize = [super collectionViewContentSize];
if (self.infiniteScrollingEnabled)
{
// We add the height (or width) of the collection view to the content size to allow us to seemlessly wrap without any screen artifacts
if (self.scrollDirection == UICollectionViewScrollDirectionVertical)
{
contentSize = CGSizeMake(contentSize.width, contentSize.height + self.collectionView.bounds.size.height + self.minimumLineSpacing);
}
else
{
contentSize = CGSizeMake(contentSize.width + self.collectionView.bounds.size.width + self.minimumLineSpacing, contentSize.height);
}
}
//if (self.centeringEnabled)
//{
contentSize.width += self.contentInsetLeftRight * 2;
//}
return contentSize;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
if (self.fastSlippingEnabled)
{
return true;
}
else if (self.infiniteScrollingEnabled)
{
if (self.scrollDirection == UICollectionViewScrollDirectionVertical)
{
if (newBounds.origin.y <= self.collectionView.bounds.size.height)
{
return YES;
}
if (newBounds.origin.y >= [super collectionViewContentSize].height - self.collectionView.bounds.size.height)
{
return YES;
}
}
else
{
if (newBounds.origin.x <= self.collectionView.bounds.size.width)
{
return YES;
}
if (newBounds.origin.x >= [super collectionViewContentSize].width - self.collectionView.bounds.size.width)
{
return YES;
}
}
}
return [super shouldInvalidateLayoutForBoundsChange:newBounds];
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
rect = CGRectMake(rect.origin.x - self.contentInsetLeftRight, rect.origin.y, rect.size.width, rect.size.height);
//NSLog(@"rect = %@", NSStringFromCGRect(rect));
NSArray* layoutAttributes = [super layoutAttributesForElementsInRect:rect];
if (self.scrollDirection == UICollectionViewScrollDirectionVertical)
{
if (self.infiniteScrollingEnabled)
{
NSArray* wrappingAttributes = [super layoutAttributesForElementsInRect:CGRectMake(rect.origin.x,
rect.origin.y - [super collectionViewContentSize].height,
rect.size.width,
rect.size.height)];
for (UICollectionViewLayoutAttributes* attributes in wrappingAttributes)
{
attributes.center = CGPointMake(attributes.center.x, attributes.center.y + [super collectionViewContentSize].height + self.minimumLineSpacing);
}
layoutAttributes = [layoutAttributes arrayByAddingObjectsFromArray:wrappingAttributes];
}
}
else
{
if (self.infiniteScrollingEnabled)
{
CGFloat superWidth = [super collectionViewContentSize].width;
NSArray* wrappingAttributes = [super layoutAttributesForElementsInRect:CGRectMake(rect.origin.x - superWidth,
rect.origin.y,
rect.size.width,
rect.size.height)];
for (UICollectionViewLayoutAttributes* attributes in wrappingAttributes)
{
attributes.center = CGPointMake(attributes.center.x + superWidth + self.minimumLineSpacing, attributes.center.y);
}
layoutAttributes = [layoutAttributes arrayByAddingObjectsFromArray:wrappingAttributes];
}
for (UICollectionViewLayoutAttributes* attributes in layoutAttributes)
{
[self correctFrameForItemAttributes:attributes];
}
}
return layoutAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* layoutAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
if (self.infiniteScrollingEnabled)
{
layoutAttributes.center = CGPointMake(layoutAttributes.center.x + self.collectionView.bounds.size.width, layoutAttributes.center.y);
}
[self correctFrameForItemAttributes:layoutAttributes];
return layoutAttributes;
}
-(void)correctFrameForItemAttributes:(UICollectionViewLayoutAttributes*)itemAttributes
{
CGRect newFrame = itemAttributes.frame;
if (self.centeringEnabled)
{
newFrame.origin.x += self.contentInsetLeftRight;
}
if (self.fastSlippingEnabled)
{
CGFloat contentOffsetX = self.collectionView.contentOffset.x;
CGFloat distance = newFrame.origin.x - contentOffsetX;
if (distance < 0 && fabs(distance)<=self.itemSize.width)
{
distance = fabs(distance);
CGFloat coef = 1.0f;
if (fabs(distance) < self.itemSize.width)
{
coef *= distance / self.itemSize.width;
}
itemAttributes.alpha = 1 - coef;
newFrame.origin.x -= self.itemSize.width * coef;
}
}
// NSLog(@"attribute (%ld - %ld)\torigin: %@\tnew origin: %@",
// (long)itemAttributes.indexPath.section,
// (long)itemAttributes.indexPath.row,
// NSStringFromCGPoint(itemAttributes.frame.origin),
// NSStringFromCGPoint(newFrame.origin));
itemAttributes.frame = newFrame;
}
- (CGFloat)contentInsetLeftRight
{
if (self.centeringEnabled) {
return (self.collectionView.bounds.size.width - self.itemSize.width) * 0.5f;
}
return 0;
}
-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
{
if (self.pagingStyle == MyCyrcleLayoutPagingStyleOff)
{
proposedContentOffset = [super targetContentOffsetForProposedContentOffset:proposedContentOffset];
}
else
{
proposedContentOffset.x = self.currentPage * self.pageWidth;
}
return proposedContentOffset;
}
#pragma mark paging handling
-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal
&& self.pagingStyle != MyCyrcleLayoutPagingStyleOff)
{
NSInteger propousedPage = round(proposedContentOffset.x / self.pageWidth);
if (self.pagingStyle == MyCyrcleLayoutPagingStyleStepping)
{
if (propousedPage < self.currentPage)
{
propousedPage = self.currentPage - 1;
}
else if (propousedPage > self.currentPage)
{
propousedPage = self.currentPage + 1;
}
}
proposedContentOffset.x = propousedPage * self.pageWidth;//candidateAttributes.center.x - self.collectionView.bounds.size.width * 0.5f;
//
// if (propousedPage < 0)
// {
// self.currentPage = numberOfPages + propousedPage;
// }
// else if (propousedPage >= numberOfPages)
// {
// self.currentPage = propousedPage - numberOfPages;
// }
NSLog(@"okd currentPage: %li; proposedPage: %li", (long)self.currentPage, (long)propousedPage);
if (!self.infiniteScrollingEnabled)
{
if (propousedPage < 0)
{
propousedPage = 0;
}
else
{
NSInteger maxPageValue = [self.collectionView numberOfItemsInSection:0] - 1;
if (propousedPage > maxPageValue)
{
propousedPage = maxPageValue;
}
}
}
self.currentPage = propousedPage;
NSLog(@"new currentPage: %li", (long)self.currentPage);
//NSLog(@"proposedContentOffset: %@ proposedPage: %d velocity: %@", NSStringFromCGPoint(proposedContentOffset), propousedPage, NSStringFromCGPoint(velocity));
}
else
{
proposedContentOffset = [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity];
}
return proposedContentOffset;
}
@end