举一反三 willDisplayCell在UICollectionView中的一些应用

简介:

一、UICollectionViewCell动画

上一篇博客写仿58同城实现UITableViewCell动画,同样UiCollectionView中也能用,上一个是从右到左的动画还比较好弄, 但如果ScrollView滑动方向时垂直的,动画方向也是垂直的话那就有一些要注意的了,因为Cell的frame是根据父视图ScrollView的ContentSize、偏移量、ContentInset决定的,所以设置frame.Y要特别注意.x也要注意不能使用0.


//
//  ViewController.m
//  CollectionCell
//
//  Created by City--Online on 15/11/9.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *showedIndexPaths;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _showedIndexPaths=[[NSMutableArray alloc]init];
    float itemWidth=(self.view.bounds.size.width-3)/4;
    UICollectionViewFlowLayout  *collectionViewFlowLayout=[[UICollectionViewFlowLayout alloc]init];
    
    collectionViewFlowLayout.minimumInteritemSpacing=1.0;
    collectionViewFlowLayout.minimumLineSpacing=0.0;
    collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0.0, 0.0, 0, 0.0);

    collectionViewFlowLayout.itemSize=CGSizeMake(itemWidth, itemWidth);
    collectionViewFlowLayout.estimatedItemSize=CGSizeMake(itemWidth, itemWidth);
    
    collectionViewFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;

    collectionViewFlowLayout.headerReferenceSize=CGSizeMake(0, 0);
    collectionViewFlowLayout.footerReferenceSize=CGSizeMake(0, 0);
    
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-itemWidth*2, self.view.bounds.size.width, itemWidth*2) collectionViewLayout:collectionViewFlowLayout];
    _collectionView.backgroundColor=[UIColor whiteColor];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.allowsSelection=YES;
    _collectionView.allowsMultipleSelection=YES;
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
   
    [self.view addSubview:_collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/ 255.0  green:arc4random()%256 / 255.0 blue:arc4random()% 256 / 255.0  alpha:1];
   
    return cell;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
    
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_showedIndexPaths containsObject:indexPath]) {
        return;
    }
    else
    {
        [_showedIndexPaths addObject:indexPath];
        NSLog(@"%@",NSStringFromCGRect(cell.frame));
        
        CGRect toFrame=cell.frame;
        //Y ScrollView滑动到底部的时的Y
        cell.frame=CGRectMake(cell.frame.origin.x,_collectionView.contentSize.height+_collectionView.contentOffset.y+_collectionView.contentInset.top, cell.bounds.size.width, cell.bounds.size.height);
        cell.layer.cornerRadius=cell.bounds.size.width/2;

        [UIView animateWithDuration:(indexPath.row)*0.2+0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            cell.frame=toFrame;
        } completion:^(BOOL finished) {
           
        }];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果图:

二、仿百度 右边一个Cell,左边两个Cell

在一些APP中也会有下面右边一个Cell,左边两个Celll类似的布局,除了用UICollectionViewFlowLayout自定义外,简单一点就可以在willDisplayCell中改变Cell的frame。


相关文章
|
传感器 监控 BI
基于STM32的智能垃圾分类系统设计与实现
基于STM32的智能垃圾分类系统设计与实现
1200 0
|
前端开发 Java 数据库
基于SpringBoot的生鲜交易系统
基于SpringBoot的生鲜交易系统
|
数据可视化
绘制热图时看不出颜色差异?四种方式转换处理使结果显而“易”见
绘制热图时看不出颜色差异?四种方式转换处理使结果显而“易”见
20984 2
|
网络协议
NTP网络协议校时
实际业务需求中由于两台server的时间不一致。导致漏数据。损失巨大,被user批的不行不行的。还好IT有一台 server 专门用来校时。
513 0
|
11月前
Dbeaver迁移配置
Dbeaver迁移配置
661 5
Dbeaver迁移配置
Swift 中 struct(结构体)和 class(类)的区别
【10月更文挑战第10天】理解 struct 和 class 的区别对于正确使用 Swift 语言进行编程非常重要。在实际开发中,需要根据具体的需求和场景来选择合适的数据类型,以充分发挥它们的优势,提高代码的质量和效率。
|
机器学习/深度学习 算法 前端开发
基于Python深度学习果蔬识别系统实现
本项目基于Python和TensorFlow,使用ResNet卷积神经网络模型,对12种常见果蔬(如土豆、苹果等)的图像数据集进行训练,构建了一个高精度的果蔬识别系统。系统通过Django框架搭建Web端可视化界面,用户可上传图片并自动识别果蔬种类。该项目旨在提高农业生产效率,广泛应用于食品安全、智能农业等领域。CNN凭借其强大的特征提取能力,在图像分类任务中表现出色,为实现高效的自动化果蔬识别提供了技术支持。
503 0
基于Python深度学习果蔬识别系统实现
|
存储 iOS开发 Perl
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
1067 2
|
Java 开发者
在Java编程中,正确的命名规范不仅能提升代码的可读性和可维护性,还能有效避免命名冲突。
【10月更文挑战第13天】在Java编程中,正确的命名规范不仅能提升代码的可读性和可维护性,还能有效避免命名冲突。本文将带你深入了解Java命名规则,包括标识符的基本规则、变量和方法的命名方式、常量的命名习惯以及如何避免关键字冲突,通过实例解析,助你写出更规范、优雅的代码。
389 3
|
TensorFlow 算法框架/工具 Python
【Mac 系统】解决VSCode用Conda成功安装TensorFlow但程序报错显示红色波浪线Unable to import ‘tensorflow‘ pylint(import-error)
本文解决在Mac系统上使用VSCode时遇到的TensorFlow无法导入问题,原因是Python解析器未正确设置为Conda环境下的版本。通过在VSCode左下角选择正确的Python解析器,即可解决import TensorFlow时报错和显示红色波浪线的问题。
812 9