2017-10-19 85 views
0

我有一个水平集合视图。基本上,第一个显示的项目是“活动”项目。并将卡扣划过每个物品。UICollectionView捕捉到0,0 - 滚动过去界限

我遇到的问题是我需要能够滚动最后一个项目以捕捉所有的方式到前面。目前拖到最后时,它不再捕捉到第一个单元。

对不起,如果我没有任何意义。基本上,每个单元格的最左边的框架应该捕捉到collectionView的0,0。目前,它对于前两个单元完全适用。但在此之后,它碰到了边界并且不允许它捕捉。我希望能够拖动和最后单元格捕捉到0,0

谢谢!

看到这里的问题:

enter image description here

这里是我的布局子类:

class UserFlowLayout: UICollectionViewFlowLayout { 
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } 

    override init() { 
     super.init() 
     scrollDirection = .horizontal 
     itemSize = CGSize(width: 70, height: 90) 
     minimumInteritemSpacing = 15.0 
     minimumLineSpacing = 15.0 
    } 

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { 
     guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } 

     var offsetAdjustment = CGFloat.greatestFiniteMagnitude 
     let horizontalOffset = proposedContentOffset.x + collectionView.contentInset.left 

     let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height) 

     let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect) 

     layoutAttributesArray?.forEach({ (layoutAttributes) in 
      let itemOffset = layoutAttributes.frame.origin.x 
      if fabsf(Float(itemOffset - horizontalOffset)) < fabsf(Float(offsetAdjustment)) { 
       offsetAdjustment = itemOffset - horizontalOffset 
      } 
     }) 

     return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y) 
    } 

} 

回答

0

在我看来就像它试图比它可以进一步向右滚动。您需要拓宽滚动视图以在右侧包含空白区域。

您当前的屏幕宽度似乎是4个单元格+ X,因此您需要在最后一个单元格之后的空白空间的+ X。