溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

IOS中UICollectionView中Decoration View的使用

發(fā)布時間:2020-08-03 16:27:14 來源:網(wǎng)絡(luò) 閱讀:10012 作者:zfajqp 欄目:移動開發(fā)

Decoration View是UICollectionView的裝飾視圖。蘋果官方給的案例都沒涉及到這個視圖的使用。沒有具體的細節(jié)。我今天用UICollectionView做了一個簡易的書架。主要是Decoration View的使用方法。

效果如下:

IOS中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的布局加入可見區(qū)域布局。

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的設(shè)計。

首先要繼承

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。就可以看到上面圖上的效果了。



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI