3 回答

TA貢獻1856條經驗 獲得超5個贊
好的,答案是否定的,沒有子類化UICollectionViewFlowLayout,就無法做到這一點。
但是,對于以后閱讀此書的任何人來說,將其分類非常容易。
首先,我設置了子類調用MyCollectionViewFlowLayout,然后在界面構建器中,將集合視圖布局更改為“自定義”,然后選擇了流布局子類。
因為您是用這種方式做的,所以您無法在IB中指定項目大小等,因此在MyCollectionViewFlowLayout.m中,我有這個...
- (void)awakeFromNib
{
self.itemSize = CGSizeMake(75.0, 75.0);
self.minimumInteritemSpacing = 10.0;
self.minimumLineSpacing = 10.0;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
}
這將為我設置所有尺寸以及滾動方向。
然后 ...
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalOffset = proposedContentOffset.x + 5;
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray *array = [super layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes *layoutAttributes in array) {
CGFloat itemOffset = layoutAttributes.frame.origin.x;
if (ABS(itemOffset - horizontalOffset) < ABS(offsetAdjustment)) {
offsetAdjustment = itemOffset - horizontalOffset;
}
}
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}
這樣可以確保滾動在左側邊緣以5.0的邊距結束。
這就是我要做的。我根本不需要在代碼中設置流布局。

TA貢獻1826條經驗 獲得超6個贊
它不能很好地處理用戶輕彈。用戶快速滑動而滾動沒有移動太多的情況下,會出現動畫故障。
我提出的替代實現與以前提出的分頁相同,但是可以處理頁面之間的用戶滑動。
?#pragma mark - Pagination
?- (CGFloat)pageWidth {
? ? ?return self.itemSize.width + self.minimumLineSpacing;
?}
?- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
?{? ? ? ? ? ?
? ? ? ? CGFloat rawPageValue = self.collectionView.contentOffset.x / self.pageWidth;
? ? ? ? CGFloat currentPage = (velocity.x > 0.0) ? floor(rawPageValue) : ceil(rawPageValue);
? ? ? ? CGFloat nextPage = (velocity.x > 0.0) ? ceil(rawPageValue) : floor(rawPageValue);
? ? ? ? BOOL pannedLessThanAPage = fabs(1 + currentPage - rawPageValue) > 0.5;
? ? ? ? BOOL flicked = fabs(velocity.x) > [self flickVelocity];
? ? ? ? if (pannedLessThanAPage && flicked) {
? ? ? ? ? ? proposedContentOffset.x = nextPage * self.pageWidth;
? ? ? ? } else {
? ? ? ? ? ? proposedContentOffset.x = round(rawPageValue) * self.pageWidth;
? ? ? ? }
? ? ? ? return proposedContentOffset;
?}
?- (CGFloat)flickVelocity {
? ? ?return 0.3;
?}

TA貢獻1906條經驗 獲得超10個贊
接受答案的Swift版本。
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
var offsetAdjustment = CGFloat.greatestFiniteMagnitude
let horizontalOffset = proposedContentOffset.x
let targetRect = CGRect(origin: CGPoint(x: proposedContentOffset.x, y: 0), size: self.collectionView!.bounds.size)
for layoutAttributes in super.layoutAttributesForElements(in: targetRect)! {
let itemOffset = layoutAttributes.frame.origin.x
if (abs(itemOffset - horizontalOffset) < abs(offsetAdjustment)) {
offsetAdjustment = itemOffset - horizontalOffset
}
}
return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)
}
對Swift 3有效。
- 3 回答
- 0 關注
- 2180 瀏覽
添加回答
舉報