This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathHACollectionViewLargeLayout.m
executable file
·76 lines (56 loc) · 2.37 KB
/
HACollectionViewLargeLayout.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
//
// HACollectionViewLargeLayout.m
// Paper
//
// Created by Heberti Almeida on 04/02/14.
// Copyright (c) 2014 Heberti Almeida. All rights reserved.
//
#import "HACollectionViewLargeLayout.h"
@implementation HACollectionViewLargeLayout
- (id)init
{
if (!(self = [super init])) return nil;
[self setup];
return self;
}
-(void)awakeFromNib{
[self setup];
}
- (void)setup {
self.itemSize = [UIScreen mainScreen].bounds.size;
self.sectionInset = UIEdgeInsetsMake(0, -[UIScreen mainScreen].bounds.size.width, 0, 0);
self.minimumInteritemSpacing = 10.0f;
self.minimumLineSpacing = 4.0f;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.headerReferenceSize = [UIScreen mainScreen].bounds.size ;
self.collectionView.pagingEnabled = YES;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
{
return YES;
}
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray* array = [self layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
if (layoutAttributes.representedElementCategory != UICollectionElementCategoryCell)
continue; // skip headers
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
layoutAttributes.alpha = 0;
}
}
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
attributes.zIndex = -2;
}
return attributes;
}
@end