0

我正在使用单元格内的图像实现简单的收集视图。我想要实现的任务是当用户点击单元格时 - 应该有翻转动画,并且一些细节必须出现在同一个单元格中。UICollectionViewCell为选定/突出显示的项目添加动画

我已经尝试了很多东西来实现这一点,例如我在单元格的ContentView上添加了两个视图。当用户按下按钮时,我调用了transitionToView方法,并且一切正常,除了当列表包含超过9-10张图片时,在滚动列表后,一些单元格开始无意义地重复“翻转”视图。我关闭了dequeueReusableCellWithReuseIdentifier函数,并且一切正常,但在像iPhone4这样的老设备上,应用程序的运行速度很慢。

所以最好的解决办法,我发现是这样的:

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

但有可能当细胞变为选中要添加一些动画的事件?

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

} 

也该尝试毁了我的细胞 - 当一个或多个单元格翻转一些其他抄起吧..

所以我需要一个动画(我取得了什么),但我需要保持其他UICollectionView单元格独一无二,并且不要重复使用此翻转视图。

请大家帮忙! :)真的很绝望!

在此先感谢

一些解决方案:

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

一个临时的解决方案还不错..任何人有一些更好的建议吗?

+0

didSelectItemAtIndexPath现在效果更好..点击单元格..然后选择另一个..和先前选择的单元格翻转回正常..但仍然有其他单元格向下滚动时出现问题..(当我向后滚动并点按在“翻转”的单元格上 - 它们将不起作用,它们在collectionView的“不可见”部分翻转) – mz87 2013-05-03 00:21:45

回答

2

滚动时,“旧单元格”被重用 - 这就是您的表格视图执行得很好的原因。当然,如果有问题的单元格有转换的视图,它会显示一个。

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

相关问题