Ios 在UICollectionView中停止滚动
我正在浏览苹果UICollectionView的示例代码,我想知道是否有人可以向我解释一些东西。他们使用以下代码停止集合视图中的水平滚动:Ios 在UICollectionView中停止滚动,ios,Ios,我正在浏览苹果UICollectionView的示例代码,我想知道是否有人可以向我解释一些东西。他们使用以下代码停止集合视图中的水平滚动: - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { CGFloat offsetAdjustment = MAXFLOAT; CGFloat
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
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) {
CGFloat itemHorizontalCenter = layoutAttributes.center.x;
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
offsetAdjustment = itemHorizontalCenter - horizontalCenter;
}
}
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
-(CGPoint)targetContentOffset for proposedContentOffset:(CGPoint)proposedContentOffset与CrollingVelocity:(CGPoint)速度
{
CGFloat offsetAdjustment=MAXFLOAT;
CGFloat horizontalCenter=proposedContentOffset.x+(CGRectGetWidth(self.collectionView.bounds)/2.0);
CGRect targetRect=CGRectMake(proposedContentOffset.x,0.0,self.collectionView.bounds.size.width,self.collectionView.bounds.size.height);
NSArray*数组=[super-layouttributesforementsinrect:targetRect];
用于(UICollectionViewLayoutAttribute*数组中的LayoutAttribute){
CGFloat itemHorizontalCenter=布局属性.center.x;
if(ABS(项目水平中心-水平中心)
我不明白他们想做什么。我所理解的唯一部分是创建targetect并获取targetect中这些元素的属性。但从那以后,我不知道他们为什么这么做。这种补偿调整从何而来?为什么要使用MAXFLOAT?任何想法都将不胜感激。谢谢 代码的目的是调整滚动结束位置,使其“捕捉”到特定单元格。这是通过偏移调整
完成的MAXFLOAT
只是初始值;在循环内,偏移调整
迭代减少到目标帧中心和最靠近目标帧中间的单元格之间的水平距离
通过将目标帧偏移量返回该x量,滚动将捕捉到最近的单元格