-
Notifications
You must be signed in to change notification settings - Fork 9
/
SFSCircleLayout.m
55 lines (44 loc) · 1.91 KB
/
SFSCircleLayout.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
//
// SFSCircleLayout.m
// SFSCollectionMenu
//
// Created by BJ Miller on 9/7/13.
// Copyright (c) 2013 Six Five Software, LLC. All rights reserved.
//
#import "SFSCircleLayout.h"
#define CIRCLE_DIAMETER 70
#define IPAD_SCALE 6.0
#define IPHONE_SCALE 3.0
@implementation SFSCircleLayout
- (void)prepareLayout {
[super prepareLayout];
CGSize size = self.collectionView.frame.size;
_cellCount = [self.collectionView numberOfItemsInSection:0];
_center = CGPointMake(size.width / 2.0, size.height / 2.0);
_radius = MIN(size.width, size.height) / (([self isPhone]) ? IPHONE_SCALE : IPAD_SCALE); //3.0;
}
- (BOOL)isPhone {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone);
}
- (CGSize)collectionViewContentSize {
return self.collectionView.frame.size;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.size = CGSizeMake(CIRCLE_DIAMETER, CIRCLE_DIAMETER);
attributes.center = CGPointMake(CGRectGetMaxX(self.collectionView.frame), CGRectGetMaxY(self.collectionView.frame));
[UIView animateWithDuration:2 animations:^{
attributes.center = CGPointMake(self.center.x - self.radius * sinf(2 * indexPath.item * M_PI / self.cellCount),
self.center.y - self.radius * cosf(2 * indexPath.item * M_PI / self.cellCount));
}];
return attributes;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *attributes = [NSMutableArray array];
for (NSInteger i = 0; i < self.cellCount; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return attributes;
}
@end