UICollectionViewFlowLayout很棒,但是,我希望它稍微調整一下,以便scrollDirection與佈局方向不同。我想要的例子是跳板主屏幕或表情符號鍵盤,您可以左右滑動,但單元格從左到右,從上到下佈置(而不是從上到下,從左到右,因爲它們是UICollectionViewFlowLayout的scrollDirection設置爲水平)。任何人都知道我可以輕鬆地將FlowLayout子類化以進行此更改?我很失望,他們沒有layoutDirection除了UICollectionViewFlowLayout對象上的scrollDirection :(我可以使用scrollDirection與佈局方向不同的UICollectionViewFlowLayout嗎?
@rdelmar我實現了類似於您的解決方案的東西,並且工作起來(不像我想要的那麼優雅),但我注意到,偶爾項目從收集,除非重新繪製,這就是爲什麼我不接受答案消失,這是我結束了:
@interface UICollectionViewPagedFlowLayout : UICollectionViewFlowLayout
@property int width;
@property int height;
@end
@implementation UICollectionViewPagedFlowLayout
#warning MINOR BUG: sometimes symbols disappear
- (CGSize)collectionViewContentSize {
CGSize size = [super collectionViewContentSize];
// make sure it's wide enough to cover all the objects
size.width = self.collectionView.bounds.size.width * [self.collectionView numberOfSections];
return size;
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for (UICollectionViewLayoutAttributes* attributes in array) {
if (CGRectIntersectsRect(attributes.frame, rect)) {
// see which section this is in
CGRect configurableRect = UIEdgeInsetsInsetRect(visibleRect, self.sectionInset);
int horizontalIndex = attributes.indexPath.row % self.width;
int verticalIndex = attributes.indexPath.row/self.width;
double xspace = (configurableRect.size.width - self.width * self.itemSize.width)/self.width;
double yspace = (configurableRect.size.height - self.height * self.itemSize.height)/self.height;
attributes.center = CGPointMake(attributes.indexPath.section * visibleRect.size.width + self.sectionInset.left + (self.itemSize.width + xspace) * horizontalIndex + self.itemSize.width/2, self.sectionInset.top + (self.itemSize.height + yspace) * verticalIndex + self.itemSize.height/2);
}
}
return array;
}
@end
我重新看了兩個會話並沒有覆蓋的滾動方向獨立於UICollectionViewFlowLayout的佈局。 – Gujamin