您好,登錄后才能下訂單哦!
Decoration View是UICollectionView的裝飾視圖。蘋果官方給的案例都沒涉及到這個視圖的使用。沒有具體的細節。我今天用UICollectionView做了一個簡易的書架。主要是Decoration View的使用方法。
效果如下:
基本的UICollectionView使用方法請自己查詢。
#import "CVViewController.h"
#import "CVCell.h"
#import "CVLayout.h"
@interfaceCVViewController ()
@end
@implementation CVViewController
- (void)viewDidLoad
{
[superviewDidLoad];
[self.coll registerClass:[CVCell class] forCellWithReuseIdentifier:@"cell"];
CVLayout *layout=[[CVLayout alloc] init];
[self.coll setCollectionViewLayout:layout];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
return cell;
}
@end
其中CVCell是我自定義的一個
UICollectionViewCell
其中CVLayout是我自定義的一個
UICollectionViewLayout
接下來主要看一下自定義的layout
#import "CVLayout.h"
#import "CVDEView.h"
@implementation CVLayout
-(void)prepareLayout{
[super prepareLayout];
[self registerClass:[CVDEView class] forDecorationViewOfKind:@"CDV"];//注冊Decoration View
}
-(CGSize)collectionViewContentSize{
return self.collectionView.frame.size;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:path];
attributes.size = CGSizeMake(215/3.0, 303/3.0);
attributes.center=CGPointMake(80*(path.item+1), 62.5+125*path.section);
return attributes;
}
//Decoration View的布局。
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath{
UICollectionViewLayoutAttributes* att = [UICollectionViewLayoutAttributeslayoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
att.frame=CGRectMake(0, (125*indexPath.section)/2.0, 320, 125);
att.zIndex=-1;
return att;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
NSMutableArray* attributes = [NSMutableArrayarray];
//把Decoration View的布局加入可見區域布局。
for (int y=0; y<3; y++) {
NSIndexPath* indexPath = [NSIndexPathindexPathForItem:3inSection:y];
[attributes addObject:[selflayoutAttributesForDecorationViewOfKind:@"CDV"atIndexPath:indexPath]];
}
for (NSInteger i=0 ; i < 3; i++) {
for (NSInteger t=0; t<3; t++) {
NSIndexPath* indexPath = [NSIndexPathindexPathForItem:t inSection:i];
[attributes addObject:[selflayoutAttributesForItemAtIndexPath:indexPath]];
}
}
return attributes;
}
下面是最后的Decoration View的設計。
首先要繼承
UICollectionReusableView
然后
@implementation CVDEView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
UIImageView *p_w_picpathView=[[UIImageViewalloc] initWithFrame:frame];
p_w_picpathView.p_w_picpath=[UIImagep_w_picpathNamed:@"BookShelfCell.png"];
[selfaddSubview:p_w_picpathView];
}
returnself;
}
OK。就可以看到上面圖上的效果了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。