iOS UIcollectionView 实现卡牌翻转效果

- (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];

}

}];

}

上一篇:codevs1137 计算系数


下一篇:c/c++ double的数字 转成字符串后 可以有效的避免精度要求不高的数