0

Objective Cで水平UICollectionViewを実装しています。以下の図のように、セルをコレクションビューの中央にスナップします。アイテムが中央にスナップされると、左右のアイテムが部分的に表示されます。どうすればこれを達成できますか?Snap UICollection Objective Cを使用してセルを中心に表示

enter image description here

私は、カスタムIOCOllectionViewFlowLayoutを使用して

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity 

が、その私のために働いていない、Objective Cのを使用してそれを実装する方法

CGFloat approximatePage = self.collectionView.contentOffset.x/self.pageWidth; 
    CGFloat currentPage = (velocity.x < 0.0) ? floor(approximatePage) : ceil(approximatePage); 

    NSInteger flickedPages = ceil(velocity.x/self.flickVelocity); 
    float pageWidth = self.pageWidth; 
    if (flickedPages) { 
     proposedContentOffset.x = flickedPages * self.pageWidth; 
    } else { 
     proposedContentOffset.x = currentPage * self.pageWidth; 
    } 
    return proposedContentOffset; 

をオーバーライドしていますか?

+0

スナップすると、目的のセルをタッピングすることができますか? – Stefan

+0

いいえ、スクロールしている間は、そのセルを中心にする必要があります。 –

+0

ユーザーがスクロールを停止したとき、スクロールが悪いときです。 – Stefan

答えて

0

この場合、水平に配置する必要がある場合は、代理人メソッドscrollViewWillEndDraggingを前述のように実装する必要があります。ユーザーがスクロールを停止したことを検出すると、表示されているセルを検出し、このコードで中央に配置します。projectSelectionCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)

関連する問題