效果图如下:(圆形显示是自定义的CollectionCell)
UICollectionView 入门须知:
1⃣️、 UICollectionView :
1、需要一个layout(布局的样式)
2、初始化UICollectionView 的时候 同时需要注册cellID ,一定要与创建cell的地方,是同一个cellID
(初始化UIcollectionView 的时候,后面不能直接初始化UICollectionViewLayout ,而是使用它的子类 UICollectionViewFlowLayout来初始化)
UICollectionViewDataSource
UICollectionViewDelegate
2⃣️、与tableView一样,UICollectionView 有两个必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section; // 每组里面有多少行(item)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; // 初始化cell的方法
3⃣️、设置行高
如果大小一样,可以在UICollectionViewFlowLayout 里面设置
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
<span style="color:#333333;font-size:18px;"> images = @[@"3",@"4",@"5",@"6",@"搜索",@"1",@"110",@"111"];
</span><span style="color:#33cc00;font-size:18px;">CGFloat radius = WIDTH;
CGFloat angularSpacing = 15;
CGFloat xOffset = WIDTH/2;
CGFloat cell_width = (WIDTH-10*5)/3;
CGFloat cell_height = (WIDTH-10*5)/3;</span><span style="color:#333333;font-size:18px;">
</span><span style="color:#333333;font-size:18px;">
</span><span style="color:#ff0000;font-size:18px;">AWCollectionViewDialLayout *flowLayout = [[AWCollectionViewDialLayout alloc] initWithRadius:radius andAngularSpacing:angularSpacing andCellSize:CGSizeMake(cell_width, cell_height) andAlignment:WHEELALIGNMENTCENTER andItemHeight:cell_height andXOffset:xOffset];</span><span style="color: rgb(51, 204, 0);font-size:14px; font-family: Arial, Helvetica, sans-serif;">// 使用了</span><span style="color: rgb(51, 204, 0); font-family: Arial, Helvetica, sans-serif;">AWCollectionViewDialLayout </span><span style="color:#ff0000;font-size:18px;">
</span><span style="color:#333333;font-size:18px;">
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor grayColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
// 初始化UICollectionView 的同时,注册cell
[collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellID];
}
#pragma mark -----------UICollectionView必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return images.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",images[indexPath.row]]];
cell.nameLabel.text = images[indexPath.row];
return cell;
}</span><span style="color:#cc9933;font-size:18px;">
</span>
AWCollectionViewDialLayout
这个UICollection的布局样式的代码我们可以在github上找到,在这里,我把代码找下来供大家分享
AWCollectionViewDialLayout.h中
<span style="color:#666666;">#import <UIKit/UIKit.h>
@interface AWCollectionViewDialLayout : UICollectionViewLayout
typedef enum WheelAlignmentType : NSInteger WheelAlignmentType;
enum WheelAlignmentType : NSInteger {
WHEELALIGNMENTLEFT,
WHEELALIGNMENTCENTER
};
@property (readwrite, nonatomic, assign) int cellCount;
@property (readwrite, nonatomic, assign) int wheelType;
@property (readwrite, nonatomic, assign) CGPoint center;
@property (readwrite, nonatomic, assign) CGFloat offset;
@property (readwrite, nonatomic, assign) CGFloat itemHeight;
@property (readwrite, nonatomic, assign) CGFloat xOffset;
@property (readwrite, nonatomic, assign) CGSize cellSize;
@property (readwrite, nonatomic, assign) CGFloat AngularSpacing;
@property (readwrite, nonatomic, assign) CGFloat dialRadius;
@property (readonly, nonatomic, strong) NSIndexPath *currentIndexPath;
-(id)initWithRadius: (CGFloat) radius andAngularSpacing: (CGFloat) spacing andCellSize: (CGSize) cell andAlignment:(WheelAlignmentType)alignment andItemHeight:(CGFloat)height andXOffset: (CGFloat) xOffset;
@end</span>
AWCollectionViewDialLayout.m中
<span style="color:#666666;">#import "AWCollectionViewDialLayout.h"
@implementation AWCollectionViewDialLayout
- (id)init
{
if ((self = [super init]) != NULL)
{
[self setup];
}
return self;
}
-(id)initWithRadius: (CGFloat) radius andAngularSpacing: (CGFloat) spacing andCellSize: (CGSize) cell andAlignment:(WheelAlignmentType)alignment andItemHeight:(CGFloat)height andXOffset: (CGFloat) xOff{
if ((self = [super init]) != NULL)
{
self.dialRadius = radius;//420.0f;
self.cellSize = cell;//(CGSize){ 220.0f, 80.0f };
self.itemHeight = height;
self.AngularSpacing = spacing;//8.0f;
self.xOffset = xOff;
self.wheelType = alignment;
[self setup];
}
return self;
}
- (void)setup
{
self.offset = 0.0f;
}
- (void)prepareLayout
{
[super prepareLayout];
self.cellCount = (int)[self.collectionView numberOfItemsInSection:0];
self.offset = -self.collectionView.contentOffset.y / self.itemHeight;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *theLayoutAttributes = [[NSMutableArray alloc] init];
float minY = CGRectGetMinY(rect);
float maxY = CGRectGetMaxY(rect);
int firstIndex = floorf(minY / self.itemHeight);
int lastIndex = floorf(maxY / self.itemHeight);
int activeIndex = (int)(firstIndex + lastIndex)/2;
int maxVisibleOnScreen = 180 / self.AngularSpacing + 2;
int firstItem = fmax(0, activeIndex - (int)(maxVisibleOnScreen/2) );
int lastItem = fmin( self.cellCount-1 , activeIndex + (int)(maxVisibleOnScreen/2) );
//float firstItem = fmax(0 , floorf(minY / self.itemHeight) - (90/self.AngularSpacing) );
//float lastItem = fmin( self.cellCount-1 , floorf(maxY / self.itemHeight) );
for( int i = firstItem; i <= lastItem; i++ ){
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *theAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[theLayoutAttributes addObject:theAttributes];
}
return [theLayoutAttributes copy];
}
- (CGSize)collectionViewContentSize
{
const CGSize theSize = {
.width = self.collectionView.bounds.size.width,
.height = (self.cellCount-1) * self.itemHeight + self.collectionView.bounds.size.height,
};
return(theSize);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
double newIndex = (indexPath.item + self.offset);
UICollectionViewLayoutAttributes *theAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
theAttributes.size = self.cellSize;
float scaleFactor;
float deltaX;
CGAffineTransform translationT;
CGAffineTransform rotationT = CGAffineTransformMakeRotation(self.AngularSpacing* newIndex *M_PI/180);
if(indexPath.item == 3){
// NSLog(@"angle 3 :%f", self.AngularSpacing* newIndex);
}
if( self.wheelType == WHEELALIGNMENTLEFT){
scaleFactor = fmax(0.6, 1 - fabs( newIndex *0.25));
deltaX = self.cellSize.width/2;
theAttributes.center = CGPointMake(-self.dialRadius + self.xOffset , self.collectionView.bounds.size.height/2 + self.collectionView.contentOffset.y);
translationT =CGAffineTransformMakeTranslation(self.dialRadius + (deltaX*scaleFactor) , 0);
}else {
scaleFactor = fmax(0.4, 1 - fabs( newIndex *0.50));
deltaX = self.collectionView.bounds.size.width/2;
theAttributes.center = CGPointMake(-self.dialRadius + self.xOffset , self.collectionView.bounds.size.height/2 + self.collectionView.contentOffset.y);
translationT =CGAffineTransformMakeTranslation(self.dialRadius + ((1 - scaleFactor) * -30) , 0);
}
CGAffineTransform scaleT = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
theAttributes.alpha = scaleFactor;
theAttributes.transform = CGAffineTransformConcat(scaleT, CGAffineTransformConcat(translationT, rotationT));
theAttributes.zIndex = indexPath.item;
return(theAttributes);
}
@end</span>