3
votes

I have a collectionView which works well in iOS 7 and now in iOS 8 is acts strangely. when collectionView appears it only displays one cell: (it must be 2)

enter image description here

but after scrolling a bit the second cell appears

enter image description here

Im using a custom collectionViewFlowLayout. but changing to UICollectionViewFlowLayout doesn't fix the issue.

Cell Size : 657, 500

Min Spacing For Lines : 100

Min Spacing For Cells : 10

I have added left and right edge insets: (if I remove the insets it works well. but I must use insets to keep my cell at the center of view)

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

Here is my custom flow layout:

#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);

}

initially in overrided layoutAttributesForElementsInRect visible rect is {0,0, 1024, 768} but [super layoutAttributesForElementsInRect:rect]; returns only one UICollectionViewCellAttribute. (it should be 2)

is any idea how can I fix this?

1

1 Answers

2
votes

I don know How it can be cause of the issue but it was originated from:

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

I want to update my pageControl to indicate which cell is at the center of screen.

I Changed my method and now it works well:

//*****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;
    }
}