UICollectionView的简单布局

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/M_15915899719/article/details/73028396
UICollectionView的简单布局
如果使用的是UICollectionViewController的话,就重写init方法
// 重写init方法
- (instancetype)init {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    return [self initWithCollectionViewLayout:flowLayout];
}

初始化的时候请注意用的类,初始化用的是UICollectionViewFlowLayout类而不是UICollectionViewLayout;

接着就是注册要使用的cell、Header、footer

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID];
接下来的是代理方法

#pragma mark - UICollectionViewDelegateFlowLayout
// 个人认为:cell的Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((320-20)/3, 150);
}

/*
    个人认为:同section中纵向的两个Item的间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return 5;
}

/*
    个人认为:同section中横向的两个Item的间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}

// 个人认为:collectionView的边距 -- 上、左、下、右
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(30, 5, 5, 5);// 上.左.下.右
}

// sectionHeader、 sectionFooter
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *headerView;
    if (indexPath.section == 0) {
        headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
        if (headerView == nil) {
            headerView = [[UICollectionReusableView alloc] init];
        }
        headerView.backgroundColor = [UIColor redColor];
        [headerView addSubview:self.rotate];
    }
    return headerView;
    
    
    //    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    //        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
    //        if (headerView == nil) {
    //            headerView = [[UICollectionReusableView alloc] init];
    //        }
    //        headerView.backgroundColor = [UIColor redColor];
    //        [headerView addSubview:self.rotate];
    //        return headerView;
    //    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
    //        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerID forIndexPath:indexPath];
    //        if (footerView == nil) {
    //            footerView = [[UICollectionReusableView alloc] init];
    //        }
    //        footerView.backgroundColor = [UIColor grayColor];
    //        return footerView;
    //    }
    //    return nil;
}

// sectionFooter的Size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    return CGSizeMake(DMScreenWidth, 11);
//}

// sectionHeader的Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    CGSize size = CGSizeZero;
    if (section == 0) {
        size = CGSizeMake(DMScreenWidth, 100);
    }
    return size;
}

#pragma mark - UICollectionViewDelegate、UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    DMNormalViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.data = nil;

    NSLog(@"%@",cell);
    return cell;
}

// 旋转
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D rotation;//3D旋转
    //    rotation = CATransform3DMakeTranslation(0 ,50 ,20);
    rotation = CATransform3DMakeRotation( M_PI_4 , 0.0, 0.7, 0.4);
    //逆时针旋转
    
    rotation = CATransform3DScale(rotation, 0.8, 0.8, 1);
    
    rotation.m34 = 1.0/ 1000;
    
    cell.layer.shadowColor = [[UIColor redColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    cell.layer.transform = rotation;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.6];
    
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    
}



猜你喜欢

转载自blog.csdn.net/M_15915899719/article/details/73028396