0

I'm implementing simple collection view with images inside cells. The task that I want to achieve is when user taps on the cell - there should be flip animation and some details have to appear at the same cell.

I've tried a lot of things to achieve that, for example I've added two views on the ContentView of the cell. And when user pushes the button I called transitionToView method and everything worked fine, except that when the list contained more than 9-10 images, after scrolling the list some cells started to duplicate "flipped" view with no reason. I turned off dequeueReusableCellWithReuseIdentifier function and everything worked fine, but on older devices like iPhone4, application worked to slowly.

So the best solution i found is this:

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

    UICollectionViewCell *cell1 = [cv dequeueReusableCellWithReuseIdentifier:kCellID3 forIndexPath:indexPath];enter code here

    UIView *contents = [[UIView alloc]initWithFrame:cell1.bounds];
    contents.layer.borderColor = [[UIColor colorWithRed:0.119 green:0.108 blue:0.222 alpha:1]CGColor];
    contents.layer.borderWidth = 10.0f;
    contents.backgroundColor = [UIColor yellowColor];
    [cell1.contentView addSubview:contents];

    UIView *backgroundView = [[UIView alloc]initWithFrame:cell1.bounds];
    backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
    backgroundView.layer.borderWidth = 4.0f;
    backgroundView.backgroundColor = [UIColor greenColor];
    cell1.selectedBackgroundView = backgroundView;
    [cell1 bringSubviewToFront:cell1.selectedBackgroundView];

    return cell1;
}

But is it possible to add some animation for the event when cell becomes selected?

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell1 = [collectionView cellForItemAtIndexPath:indexPath];

    UIView *toSwitch = cell1.contentView;
    [UIView transitionFromView:toSwitch toView:cell1.selectedBackgroundView duration:0.33 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];

}

also this attempt ruins my cells - when one or more of the cells are flipped some other start to copy it..

So I need an animation (What I achieved), but I need to keep other UICollectionView cells unique and don't reuse this flipped view..

please help! :) really desperate about this!

thanks in advance

Some Solution:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(cell.selected)
    {
        [cell setSelected:NO];
        [collectionView deselectItemAtIndexPath:indexPath animated:NO];
        UIView *toSwitch = cell.contentView;
        [UIView transitionFromView:cell.selectedBackgroundView toView:toSwitch duration:0.001 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    }
}

Pretty good for a temporary solution.. anyone has some better advice?

4

1 回答 1

2

当您滚动时,“旧单元格”会被重用——这就是让您的表格视图表现良好的原因。当然,如果有问题的单元格有一个转换的视图,它将显示那个。

因此,就像您在每次调用cellForItemAtIndexPath函数时重新设置的单元格中的数据一样,您必须将正确的视图设置为可见 - 像处理数据一样记住单元格视图的状态,然后显示相应的视图当单元格出现时。

于 2013-05-03T07:13:49.780 回答