Created
March 15, 2013 11:13
-
-
Save leeprobert/5169103 to your computer and use it in GitHub Desktop.
UICollectionViewFlowLayout sub-class for layout attributes of all the items in a circle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // CollectionViewCarouselFlowLayout.m | |
| // iP2 | |
| // | |
| // Created by Lee Probert on 07/03/2013. | |
| // | |
| // | |
| #import "CollectionViewCircleFlowLayout.h" | |
| #define ITEM_SIZE 70.0f | |
| #define ACTIVE_DISTANCE 200 | |
| #define ZOOM_FACTOR 0.2 | |
| #define PADDING 20 | |
| #define MAX_ANGLE 80 | |
| #define PERSPECTIVE 1.0/-400 | |
| @interface CollectionViewCircleFlowLayout (){ | |
| UIInterfaceOrientation _orientation; | |
| CGPoint _center; | |
| CGFloat _radius; | |
| int _cellCount; | |
| } | |
| -(void)initLayoutProperties; | |
| -(UICollectionViewLayoutAttributes*)attributesForCenterPosition:(NSIndexPath *)itemIndexPath; | |
| @end | |
| @implementation CollectionViewCircleFlowLayout | |
| -(void)prepareLayout { | |
| [super prepareLayout]; | |
| [self initLayoutProperties]; | |
| } | |
| -(void)initLayoutProperties { | |
| _orientation = [[UIApplication sharedApplication] statusBarOrientation]; | |
| self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE); | |
| self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); | |
| CGSize size = self.collectionView.frame.size; | |
| _center = CGPointMake(size.width/2, size.height/2); | |
| _radius = MIN(size.width,size.height)/2.5; | |
| _cellCount = [[self collectionView] numberOfItemsInSection:0]; | |
| } | |
| -(CGSize)collectionViewContentSize { | |
| return [self collectionView].frame.size; | |
| } | |
| /* | |
| Layout all items in a circle | |
| */ | |
| -(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { | |
| UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; | |
| attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE); | |
| attributes.center = CGPointMake(_center.x + _radius * cosf(2 * indexPath.item * M_PI / _cellCount), | |
| _center.y + _radius * sinf(2 * indexPath.item * M_PI / _cellCount)); | |
| attributes.zIndex = indexPath.row; | |
| return attributes; | |
| } | |
| -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { | |
| NSMutableArray*attributes = [NSMutableArray array]; | |
| for(NSInteger i=0; i < _cellCount; i++) { | |
| NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0]; | |
| [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; | |
| } | |
| return attributes; | |
| } | |
| /* | |
| Methods for inserting and deleting items | |
| */ | |
| - (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath { | |
| UICollectionViewLayoutAttributes* attributes = [self attributesForCenterPosition:itemIndexPath]; | |
| return attributes; | |
| } | |
| - (UICollectionViewLayoutAttributes*)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath { | |
| UICollectionViewLayoutAttributes* attributes = [self attributesForCenterPosition:itemIndexPath]; | |
| return attributes; | |
| } | |
| -(UICollectionViewLayoutAttributes*)attributesForCenterPosition:(NSIndexPath *)itemIndexPath { | |
| UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; | |
| attributes.alpha = 0.0f; | |
| attributes.center = CGPointMake(_center.x, _center.y); | |
| return attributes; | |
| } | |
| - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { | |
| return YES; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment