UICollection视图单元格翻转效果

时间:2015-10-07 13:26:03

标签: ios uicollectionview

我正在使集合视图单元格翻转并提供有关单元格的信息。

故事板:

-Collectionviewcell
-- View1 (tag 100)
-- View2 (tag 200)
didSelectItemAtIndexPath 上的

代码。

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UIView *firstView = (UIView *)[cell viewWithTag:200];
UIView *secondView = (UIView *)[cell viewWithTag:100];
NSString *indexValue = [flipIndex objectAtIndex:indexPath.row];
if (![indexValue isEqualToString:@"1"])
{
    [UIView animateWithDuration:1.0 animations:^{
        NSLog(@"ANIMATION STARTED");
        [UIView transitionFromView:secondView
                            toView:firstView
                          duration:2.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
                              [firstView setHidden:NO];
                              [secondView setHidden:YES];
                          }];
    }completion:^(BOOL finished) {
        NSLog(@"ANIMATION COMPLETED");
    }];

    [flipIndex removeObjectAtIndex:indexPath.row];
    [flipIndex insertObject:@"1" atIndex:indexPath.row];
} else
{
    [UIView animateWithDuration:1.0 animations:^{
        NSLog(@"ANIMATION STARTED");
        [UIView transitionFromView:firstView
                            toView:secondView
                          duration:2.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
                              [secondView setHidden:NO];
                              [firstView setHidden:YES];
                          }];
    }completion:^(BOOL finished) {
        NSLog(@"ANIMATION COMPLETED");
    }];

我在这里面临的问题是,首次点击视图会从view1翻转到视图2.在第二次点击view2时,view1为空,因为它为零。

帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您可以使用两个UIView出口属性为UICollectionViewCell创建子类,然后将它们连接到故事板中。

customCell.view1
customCell.view2

然后可以在不依赖标签的情况下访问它们,也不会是nil,然后不依赖于flipIndex(我认为你在做什么?)来确定哪个可以显示:

if (customCell.view1.hidden) {
      //flip to 1
} else {
      //flip to 2
}

请记住在重复使用时重置视图。

答案 1 :(得分:1)

试试这个:

[UIView transitionWithView:cell.contentView
    duration:1
    options:UIViewAnimationOptionTransitionFlipFromLeft
    animations:^{
    if (cell.isFirstView) {
        cell.isFirstView = NO;

        firstView.hidden = NO;
        secondView.hidden=YES;
    } else {
        cell.isFirstView = YES;

        firstView.hidden = YES;
        secondView.hidden = NO;
    }
} completion:nil];