UICollectionViewCell在iOS 8中消失

时间:2014-12-30 12:50:33

标签: ios uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个在iOS 7中运行良好的collectionView,现在在iOS 8中运行很奇怪。 当collectionView出现时,它只显示一个单元格:(必须是2)

enter image description here

但滚动了一下后出现第二个单元格

enter image description here

我正在使用自定义collectionViewFlowLayout。但是更改为UICollectionViewFlowLayout并不能解决问题。

细胞大小:657,500

行的最小间距:100

细胞最小间距:10

我添加了左右边缘插图:(如果我删除了插图,它运行良好。但是我必须使用插图来保持我的单元格位于视图的中心)

- (UIEdgeInsets)collectionView:(UICollectionView *)cv
                        layout:(UICollectionViewLayout *)cvl
        insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(0, (cv.bounds.size.width - 657) / 2.0f, 0,
                            (cv.bounds.size.width - 657) / 2.0f);
}

这是我的自定义流程布局:

#import "CoverFlowLayout.h"

static const CGFloat kMaxDistancePercentage = 0.3f;
//static const CGFloat kMaxRotation = (CGFloat)(50.0 * (M_PI / 180.0));
static const CGFloat kMaxZoom = 0.1f;

@implementation CoverFlowLayout

- (id)init {
    if ((self = [super init])) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.minimumLineSpacing = 10000.0f; }
    return self;
}

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
    // 1
    CGRect visibleRect =
    (CGRect){.origin = self.collectionView.contentOffset,
        .size = self.collectionView.bounds.size};
    CGFloat maxDistance =
    visibleRect.size.width * kMaxDistancePercentage;
    // 2
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes *attributes in array) {
        // 3
        CGFloat distance =
        CGRectGetMidX(visibleRect) - attributes.center.x;
        // 4
        CGFloat normalizedDistance = distance / maxDistance;
        normalizedDistance = MIN(normalizedDistance, 1.0f);
        normalizedDistance = MAX(normalizedDistance, -1.0f);
        // 5
        //CGFloat rotation = normalizedDistance * kMaxRotation;
        CGFloat zoom = 1.0f + ((1.0f - ABS(normalizedDistance)) * kMaxZoom);
        // 6
        CATransform3D transform = CATransform3DIdentity;
        transform.m34 = 1.0 / -1000.0;
        //transform = CATransform3DRotate(transform,
          //                             rotation, 0.0f, 1.0f, 0.0f);
        transform = CATransform3DScale(transform, zoom, zoom, zoom);
        attributes.transform3D = transform;
    }
    // 7
    return array;

}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}

- (CGPoint)targetContentOffsetForProposedContentOffset: (CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
    // 1
    CGFloat offsetAdjustment = CGFLOAT_MAX;
    CGFloat horizontalCenter = proposedContentOffset.x +
    (CGRectGetWidth(self.collectionView.bounds) / 2.0f);
    // 2
    CGRect targetRect = CGRectMake(proposedContentOffset.x,
                                   0.0f, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    NSArray *array =
    [super layoutAttributesForElementsInRect:targetRect];
    for (UICollectionViewLayoutAttributes* layoutAttributes in array)
    {
        // 3
        CGFloat distanceFromCenter = layoutAttributes.center.x - horizontalCenter;
        if (ABS(distanceFromCenter) < ABS(offsetAdjustment))
        {
            offsetAdjustment = distanceFromCenter;
        }
    }
    // 4
    return CGPointMake(proposedContentOffset.x + offsetAdjustment,
                   proposedContentOffset.y);

}

最初在覆盖的layoutAttributesForElementsInRect可见矩形是{0,0,1024,768} 但是[super layoutAttributesForElementsInRect:rect];只返回一个UICollectionViewCellAttribute。 (它应该是2)

我知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我不知道这是怎么回事,但它源于:

NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:midPoint];

我想更新我的pageControl以指示哪个单元格位于屏幕中心。

我改变了我的方法,现在效果很好:

//*****updating page control*****
// get the visible rect
CGRect visibleRect = (CGRect) {.origin = self.collectionView.contentOffset,
    .size = self.collectionView.bounds.size};
// get the mid point in the visible rect
CGPoint midPoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
// find indexPath of the item in that midPoint
 //in iOS 8 Cause the second cell disappear
//NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:midPoint];
//iterating through visble cells to find the cell which contains midpoint then get get that cell indexpath
for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
    if (CGRectContainsPoint(cell.frame, midPoint)) {
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

        //update page control
        self.pageControl.currentPage = indexPath.row;
        //quiting loop
        break;
    }
}