Skip to content

Instantly share code, notes, and snippets.

@wess
Created January 8, 2013 19:49
Show Gist options
  • Save wess/4487285 to your computer and use it in GitHub Desktop.
Save wess/4487285 to your computer and use it in GitHub Desktop.
My UICollectionViewLayout subclass
//
// BJUPortraitLayout.m
// BizjournalsUniversal
//
// Created by Wess Cope on 1/7/13.
// Copyright (c) 2013 ACBJ. All rights reserved.
//
#import "BJUPortraitLayout.h"
#import "Archimedes.h"
static CGFloat const kBJUCollectionLayoutTopHeight = 300.0f;
@interface BJUPortraitLayout()
@property (strong, nonatomic) NSMutableArray *pendingAttributes;
@end
@implementation BJUPortraitLayout
- (id)init
{
self = [super init];
if (self)
{
self.collectionView.pagingEnabled = YES;
self.pendingAttributes = [NSMutableArray new];
}
return self;
}
//- (void)prepareLayout
//{
//
//}
- (CGSize)collectionViewContentSize
{
CGFloat width = self.collectionView.numberOfSections * self.collectionView.width;
CGFloat height = self.collectionView.height;
return CGSizeMake(width, height);
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSIndexPath *headlinePath = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *adPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *subArticleOnePath = [NSIndexPath indexPathForRow:2 inSection:0];
NSIndexPath *subArticleTwoPath = [NSIndexPath indexPathForRow:3 inSection:0];
NSIndexPath *subArticleThreePath = [NSIndexPath indexPathForRow:4 inSection:0];
NSIndexPath *subArticleFourPath = [NSIndexPath indexPathForRow:5 inSection:0];
UICollectionViewLayoutAttributes *headlineAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:headlinePath];
UICollectionViewLayoutAttributes *adAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:adPath];
UICollectionViewLayoutAttributes *subArticleOneAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleOnePath];
UICollectionViewLayoutAttributes *subArticleTwoAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleTwoPath];
UICollectionViewLayoutAttributes *subArticleThreeAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleThreePath];
UICollectionViewLayoutAttributes *subArticleFourAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:subArticleFourPath];
CGRect visibleRect = rect;
CGFloat subArticleWidth = (visibleRect.size.width / 2);
CGFloat subArticleHeight = ((visibleRect.size.height - kBJUCollectionLayoutTopHeight) / 2);
CGRect headlineFrame = CGRectMake(visibleRect.origin.x, 0.0f, visibleRect.size.width - kBJUCollectionLayoutTopHeight, kBJUCollectionLayoutTopHeight);
CGRect adFrame = CGRectMake(headlineFrame.size.width, 0.0f, kBJUCollectionLayoutTopHeight, kBJUCollectionLayoutTopHeight);
CGRect subArticleOneFrame = CGRectMake(visibleRect.origin.x, kBJUCollectionLayoutTopHeight, subArticleWidth, subArticleHeight);
CGRect subArticleTwoFrame = CGRectMake(subArticleWidth, kBJUCollectionLayoutTopHeight, subArticleWidth, subArticleHeight);
CGRect subArticleThreeFrame = CGRectMake(visibleRect.origin.x, kBJUCollectionLayoutTopHeight + subArticleHeight, subArticleWidth, subArticleHeight);
CGRect subArticleFourFrame = CGRectMake(subArticleWidth, kBJUCollectionLayoutTopHeight + subArticleHeight, subArticleWidth, subArticleHeight);
headlineAttributes.frame = headlineFrame;
adAttributes.frame = adFrame;
subArticleOneAttributes.frame = subArticleOneFrame;
subArticleTwoAttributes.frame = subArticleTwoFrame;
subArticleThreeAttributes.frame = subArticleThreeFrame;
subArticleFourAttributes.frame = subArticleFourFrame;
headlineAttributes.center = CGRectCenterPoint(headlineFrame);
adAttributes.center = CGRectCenterPoint(adFrame);
subArticleOneAttributes.center = CGRectCenterPoint(subArticleOneFrame);
subArticleTwoAttributes.center = CGRectCenterPoint(subArticleTwoFrame);
subArticleThreeAttributes.center = CGRectCenterPoint(subArticleThreeFrame);
subArticleFourAttributes.center = CGRectCenterPoint(subArticleFourFrame);
headlineAttributes.size = headlineFrame.size;
adAttributes.size = adFrame.size;
subArticleOneAttributes.size = subArticleOneFrame.size;
subArticleTwoAttributes.size = subArticleTwoFrame.size;
subArticleThreeAttributes.size = subArticleThreeFrame.size;
subArticleFourAttributes.size = subArticleFourFrame.size;
self.pendingAttributes = [@[headlineAttributes, adAttributes, subArticleOneAttributes, subArticleTwoAttributes, subArticleThreeAttributes, subArticleFourAttributes] mutableCopy];
return self.pendingAttributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger index = [indexPath indexAtPosition:1];
UICollectionViewLayoutAttributes *layoutAttributes = self.pendingAttributes[index];
return layoutAttributes;
}
//- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//{
// NSUInteger index = [itemIndexPath indexAtPosition:1];
// UICollectionViewLayoutAttributes *layoutAttributes = self.pendingAttributes[index];
//
// return layoutAttributes;
//}
//
//- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
//{
// NSUInteger index = [itemIndexPath indexAtPosition:1];
// UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:itemIndexPath];
// UICollectionViewLayoutAttributes *layoutAttributesOrigin = self.pendingAttributes[index];
//
// layoutAttributes.size = layoutAttributesOrigin.size;
// layoutAttributes.center = layoutAttributesOrigin.center;
//
// return layoutAttributes;
//}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment