UICollectionViewCell透视变换出现/消失

时间:2014-09-25 03:16:26

标签: ios uicollectionview catransform3d

我的目标是创建一个UICollectionView,就像下图中的那个一样,它具有在退出时退出并进入视角的单元格。我的scrollViewDidScroll委托函数如下:

NSArray *rows = [self.feedCollectionView indexPathsForVisibleItems];
for (NSIndexPath *path in rows) {
    if (path.row != 1) {
        continue;
    }
    UICollectionViewCell *cell = [self.feedCollectionView cellForItemAtIndexPath:path];

    float height = cell.frame.size.height;
    float position = cell.frame.origin.y - scrollView.contentOffset.y;
    float offsetFromBottom = self.feedCollectionView.frame.size.height- position;
    float fraction = MAX(((height - offsetFromBottom)/height), 0);
    //float angleFormula = -((height - offsetFromBottom)/height)*M_PI/2;
    float rotation = MIN(-((height - offsetFromBottom)/height)*M_PI/2, 0);

    CGRect frame = cell.frame;
    [self setAnchorPoint:CGPointMake(0.5, 0.0) forView:cell];
    cell.frame = frame;

    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0/(height * 4.6666667);
    transform = CATransform3DRotate(transform, rotation, 1, 0, 0);

    [cell.layer setAnchorPoint:CGPointMake(0.5, 0.0)];
    cell.layer.sublayerTransform = transform;

    float delta = asinf(fraction);

    [cell.layer setTransform:CATransform3DMakeRotation(delta, 1, 0, 0)];
}

不幸的是,当setAnchorPoint重新加载并且整个collectionView变得混乱时,它会将细胞移出它们的自然位置。但是,不将锚点设置到新位置使得无法获得良好的透视变换,因为它确保了单元格的上边缘不会变宽。

原来在屏幕上的细胞很好,直到它们消失并重新出现,此时框架神秘地改变了。屏幕上没有的所有单元最初都有问题。看来UICollectionView在我的单元格上设置anchorPoint属性有问题。是否有任何方法可以修改透视转换以使其像anchorPoint不同,或者是一种让collectionview接受其单元格可能具有不同锚点的方法?

desc

1 个答案:

答案 0 :(得分:0)

我猜是回答我自己的问题。

解决方案是执行转换并调整单元格contentView上的锚点。