2

我有一個collectionView,它在iOS 7中運行良好,現在在iOS 8中的行爲很奇怪。 時的CollectionView它出現只顯示一個單元:(它必須是2)UICollectionViewCell在iOS 8中消失

enter image description here

但滾動有點第二小區之後出現使用定製collectionViewFlowLayout

enter image description here

林。但更改爲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); 

} 

最初overrided layoutAttributesForElementsInRect可見矩形是{0,0,1024,768} 但[super layoutAttributesForElementsInRect:rect];只返回一個UICollectionViewCellAttribute。 (應該是2)

是什麼想法我該如何解決這個問題?

回答

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