UICollectionView align logic missing in horizontal paging scrollview

前端 未结 16 1205
天命终不由人
天命终不由人 2020-12-07 06:49

I\'ve got a UICollectionView, which works ok, until I start scrolling. Here some pics first: \"enter

16条回答
  •  天涯浪人
    2020-12-07 07:30

    Do you roll your own UICollectionViewFlowLayout?

    If so, adding -(CGPoint) targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity will help you to calculate where the scrollview should stop.

    This might work (NB: UNTESTED!):

    -(CGPoint) targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
                                 withScrollingVelocity:(CGPoint)velocity
    {
      CGFloat offsetAdjustment = MAXFLOAT;
      CGFloat targetX = proposedContentOffset.x + self.minimumInteritemSpacing + self.sectionInset.left;
    
      CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
    
      NSArray *array = [super layoutAttributesForElementsInRect:targetRect];
      for(UICollectionViewLayoutAttributes *layoutAttributes in array) {
    
        if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
          CGFloat itemX = layoutAttributes.frame.origin.x;
    
          if (ABS(itemX - targetX) < ABS(offsetAdjustment)) {
            offsetAdjustment = itemX - targetX;
          }
        }
      }
    
      return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
    }
    

提交回复
热议问题