- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"卡牌翻转效果";

self.edgesForExtendedLayout = UIRectEdgeNone;

[self.view addSubview:self.collectionView];

NSArray * arr = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"];

[arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

Model *model =[[Model alloc] init];

model.select = [obj boolValue];

[self.dataArray addObject:model];

}];

[self.collectionView reloadData];

}

-(NSMutableArray *)dataArray{

if (!_dataArray) {

_dataArray = [NSMutableArray array];

}

return _dataArray;

}

-(UICollectionView *)collectionView{

if (!_collectionView) {

UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];

// 定义大小

layout.itemSize = CGSizeMake((appWidth - 40)/2.0, 0.8*(appWidth - 40)/2.0);

// 设置最小行间距

layout.minimumLineSpacing = 10;

// 设置垂直间距

layout.minimumInteritemSpacing = 10;

layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);

// 设置垂直间距

layout.headerReferenceSize = CGSizeMake(0, 0);

// 设置滚动方向(默认垂直滚动)

layout.scrollDirection = UICollectionViewScrollDirectionVertical;

_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, appWidth, appHeight - 64) collectionViewLayout:layout];

_collectionView.backgroundColor = [UIColor clearColor];

_collectionView.delegate = self;

_collectionView.dataSource = self;

[_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"homeCell"];

}

return _collectionView;

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return 1;

}

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

return self.dataArray.count;

}

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

HomeCollectionViewCell * cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCell" forIndexPath:indexPath];

Model * model = self.dataArray[indexPath.item];

if (model.select == YES) {

cell.secondView.hidden = YES;

cell.firstview.hidden = NO;

}else{

cell.secondView.hidden = NO;

cell.firstview.hidden = YES;

}

cell.bt.tag = indexPath.item;

[cell.bt addTarget:self action:@selector(didBtAction:event:) forControlEvents:UIControlEventTouchUpInside];

return cell;

}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{

Model* model = self.dataArray[indexPath.item];

HomeCollectionViewCell * cell1 = (HomeCollectionViewCell *)cell;

if (model.select == YES) {

cell1.secondView.hidden = YES;

cell1.firstview.hidden = NO;

}else{

cell1.secondView.hidden = NO;

cell1.firstview.hidden = YES;

}

}

- (void)didBtAction:(UIButton *)sender event:(UIEvent *)event{

NSSet *touches = [event allTouches];

UITouch *touch = [touches anyObject];

CGPoint position = [touch locationInView:self.collectionView];

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:position];

//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];

HomeCollectionViewCell *cell = (HomeCollectionViewCell*)[self.collectionView cellForItemAtIndexPath:indexPath];

//这里时查找视图里的子视图(这种情况查找,可能时因为父视图里面不只两个视图)

//    NSInteger fist= [[cell subviews] indexOfObject:[cell viewWithTag:1000]];

//    NSInteger seconde= [[cell subviews] indexOfObject:[cell viewWithTag:2000]];

Model* model = self.dataArray[indexPath.item];

// 3、3D翻转动画

[UIView animateWithDuration:1.0 animations:^{

if (model.select == YES)

{

cell.secondView.hidden = NO;

cell.firstview.hidden = YES;

model.select = NO;

[self.dataArray replaceObjectAtIndex:indexPath.item withObject:model];// 当前显示的是正面视图

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:cell cache:YES];

}

else

{

cell.secondView.hidden = YES;

cell.firstview.hidden = NO;

model.select = YES;

[self.dataArray replaceObjectAtIndex:indexPath.item withObject:model];// 当前显示的是背面视图

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell cache:YES];

}

}];

}

最新文章

  1. 浅谈css的伪元素::after和::before
  2. SharePoint 2013 母版页取消和HTML页关联
  3. android 项目中出现红色感叹号的解决方法
  4. HTML中表格元素TABLE,TR,TD及属性的语法
  5. json 入门(1)
  6. Kth Largest Element in an Array
  7. 使用Grunt启动和运行
  8. Sklearn库例子3:分类——岭回归分类(Ridge Regression )例子
  9. Spring MVC详细运行流程
  10. (转)理解SQL SERVER中的分区表
  11. Retrofit网络请求库应用02——json解析
  12. BITE
  13. 解决ubuntu下firefox无法在线播放音频和视频的问题
  14. BZOJ3105-新Nim游戏
  15. Day3-scrapy爬虫下载图片自定义名称
  16. YQCB冲刺第二周第六天
  17. Xcode SVN配置
  18. js备忘录_1
  19. Delphi控件备份工具
  20. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 错误处理

热门文章

  1. Vue Springboot (包括后端解决跨域)实现登录验证码功能详细完整版
  2. 【P4178】Tree——点分治
  3. 2018年第九届蓝桥杯【C++省赛B组】B、C、D、F、G 题解
  4. Matlab 数值计算
  5. 深度阅读:大学生课外知识补充,这些课堂上不教的 C++ 的基本特性你都知道吗?
  6. redis 压测与乐观锁
  7. MySQL必知必会:简介undo log、truncate、以及undo log如何帮你回滚事物
  8. C++/Java小白解Leetcode题,发现了知识盲区……
  9. LeetCode 018 4Sum
  10. FPGA 流水灯