ios UICollectionView的使用

來源:互聯網
上載者:User

ios UICollectionView的使用

UICollectionView的使用有兩種方法,一種是繼承UICollectionViewController,這個Controller會內建一個UICollectionView;另外一種是作為一個視圖放在普通的UIViewController裡面。

個人更喜歡第二種。下面採用第二種方式簡單介紹一下UICollectionView的使用。

1.UIViewController實現委託,代碼如下

@interface YourViewController : UIViewController

2.聲明UICollectionView,代碼如下

@property(nonatomic,retain)UICollectionView*myCollectionView;

3.初始化UICollectionView,代碼如下

- (void)viewDidLoad
{
[super viewDidLoad];
[self initConllectionView];
}
-(void)initConllectionView{
CircleLayout*layout=[[CircleLayout alloc] init];
myCollectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
[myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
myCollectionView.backgroundColor=[UIColor whiteColor];
myCollectionView.delegate=self;
myCollectionView.dataSource=self;
[self.view addSubview:myCollectionView];
[layout release];
}

這裡面的CircleLayout繼承自UICollectionViewLayout,主要用來表現UICollectionView的布局以及一些屬性。

4.實現- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:CELL_STR forIndexPath:indexPath];
for (UIView*view in cell.contentView.subviews) {
if (view) {
[view removeFromSuperview];
}
}
UIImageView*imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ITEM_WIDTH, ITEM_WIDTH)];
if (indexPath.row>4) {
imgView.image=[UIImage imageNamed:@"apple.png"];
}else{
imgView.image=[UIImage imageNamed:@"applec.png"];
}
[cell.contentView addSubview:imgView];
[imgView release];
return cell;
}

5.cell的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(130,130);
}

 

CircleLayout的代碼:

CircleLayout.h

#import
#import
@interface CircleLayout : UICollectionViewLayout
@property (nonatomic, assign) CGPoint center;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) NSInteger cellCount;
@end

 

CircleLayout.m

 

#define ITEM_SIZE 130
#import "CircleLayout.h"

@implementation CircleLayout
@synthesize cellCount,center,radius;
- (void)prepareLayout{
[super prepareLayout];
CGSize size = self.collectionView.frame.size;
cellCount=[self.collectionView numberOfItemsInSection:0];
center=CGPointMake(size.width/2, size.height/2);
radius = MIN(size.width, size.height) / 2.5;
}
-(CGSize)collectionViewContentSize{
return [self collectionView].frame.size;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
attributes.center = CGPointMake(center.x + radius * cosf(2 * path.item * M_PI /cellCount),

center.y + radius * sinf(2 * path.item * M_PI /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;
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath *)itemIndexPath{
UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
attributes.alpha = 0.0;
attributes.center = CGPointMake(center.x, center.y);
return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
attributes.alpha = 0.0;
attributes.center = CGPointMake(center.x, center.y);
attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0);
return attributes;
}
@end


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.