iOS UIcollectionView 实现卡牌翻转效果

Posted 执笔葬青春

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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];

        }

    }];

 

}

 

以上是关于iOS UIcollectionView 实现卡牌翻转效果的主要内容,如果未能解决你的问题,请参考以下文章

计算 iOS 中水平 UICollectionView 的动态单元格高度

IOS 瀑布流UICollectionView实现

iOS UICollectionView 实现轮播图

iOS UICollectionView 实现轮播图

ios uicollectionview怎么实现滚动

当 UICollectionView 位于 iOS 中的自定义 TableViewCell 中时,如何为 UICollectionView 实现委托和数据源方法,目标 C