4

私は、UICollectionViewFlowLayoutをサブクラス化して、ページングのような振る舞いで水平UICollectionViewを取得しました。 UICollectionViewCellが最初の最後のセルでない限り、完全に正常に動作します。下に添付された画像。UICollectionViewの最初と最後のセルにパディングを追加する

enter image description here enter image description here

私は次のように加えて、私のUICollectionViewFlowLayoutで何かを上書きする必要がありますか?

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity 
{ 
CGFloat offSetAdjustment = MAXFLOAT; 
CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width/2.0)); 

CGRect targetRect = CGRectMake(proposedContentOffset.x, 
           0.0, 
           self.collectionView.bounds.size.width, 
           self.collectionView.bounds.size.height); 

NSArray *array = [self layoutAttributesForElementsInRect:targetRect]; 
for (UICollectionViewLayoutAttributes *layoutAttributes in array) 
{ 
    if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) 
    { 
     CGFloat itemHorizontalCenter = layoutAttributes.center.x; 
     if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment)) 
     { 
      offSetAdjustment = itemHorizontalCenter - horizontalCenter; 
     } 
    } 
} 

CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment; 

do { 
    proposedContentOffset.x = nextOffset; 
    CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x; 
    CGFloat velX = velocity.x; 

    if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0)) 
    { 
     break; 
    } 

    if(velocity.x > 0.0) 
    { 
     nextOffset += [self snapStep]; 
    } 
    else if(velocity.x < 0.0) 
    { 
     nextOffset -= [self snapStep]; 
    } 
} while ([self isValidOffset:nextOffset]); 

proposedContentOffset.y = 0.0; 

return proposedContentOffset; 
} 
    - (BOOL)isValidOffset:(CGFloat)offset 
{ 
    return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]); 
} 

- (CGFloat)minContentOffset 
{ 
    return -self.collectionView.contentInset.left; 
} 

- (CGFloat)maxContentOffset 
{ 
    return [self minContentOffset] + self.collectionView.contentSize.width -  self.itemSize.width; 
} 

- (CGFloat)snapStep 
{ 
return self.itemSize.width + self.minimumLineSpacing; 
} 

任意のポインタ/コメントが便利です。

+0

あなたは 'UIEdgeInsetsを使用することができます'UICollectionViewFlowLayout'のsectionInset'プロパティです。この[post](http://stackoverflow.com/questions/16934831/placing-a-margin-around-each-edge-of-the-uicollectionview)を参照してください。また、[セクションインセットの使用](https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/FlowLayout.htmlを参照)のApple Documentationを参照してください。 –

答えて

14

コレクションビューのフレームを設定するときに、左右のスペースをパディングと同じに設定できます。

または

あなたはそれが最初のセルまたは最後のセルであれば、それに応じてパディングを管理することをcellForItemAtIndexPathに条件を置くことができます。それでおしまい。

それとも

あなたcollectionViewのcontentInsetプロパティを設定することができます。例えば

、あなたが怒鳴る法のようなUIEdgeInsetsを設定するcollectionviewメソッドを使用することができます

UICollectionView *cv; // your collectionView 

cv.contentInset = UIEdgeInsetsMake(0, 5, 0, 5); 
+0

私は最後にセクションインセットを使用しています30分は、UICollectionView自体のインセットを考えなかった。 – slonkar

+0

ええ、ContentInsetを設定することは、あなたの場合には良い解決策です。 :) – Lion

+0

ありがとう、この回答は私をたくさん助けました –

1

シンプル。

あなたが最初と最後のセルの左側のスペースと右サイ​​ドのスペースに値を渡すことができ、ここで
-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    return UIEdgeInsetsMake(0,10,0,10); // top, left, bottom, right 
} 

、あなたも怒鳴るメソッドを介して2つのセル間の最小スペースを提供することができます

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    return 5.0; 
} 
+0

UICollectionViewFlowLayoutのインスタンスを作成するときに、すでにインセットを設定しています。したがって、私は水平スクロールを行っているので、これは役に立ちません。また、minimumInteritemSpacingはここでは役に立ちません。私は既に行っているminimumLineSpacingをここで使用する必要があります。 – slonkar

関連する問題