1.彈幕:
https://github.com/zkfpk6/YGFlyCommentManager-Danmaku
2.瀑布流:
- (void)prepareLayout; 準備布局cell
先存儲各列MaxY值
再循環(huán)執(zhí)行 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath,設置新的cell布局屬性并存儲
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;? 改變cell布局屬性
已知寬度,讓代理根據(jù)實際內(nèi)容計算高度返回,至此最新的size可得到,然后遍歷MaxY數(shù)組,找出最短的是哪一列, 最后根據(jù)最短列 計算出X 、Y值,設置UICollectionViewLayoutAttributes的frame屬性,并更新MaxY數(shù)組。
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;返回所有的cell布局屬性
返回所有cell的布局屬性(prepareLayout中存儲的)
- (CGSize)collectionViewContentSize;
設置ContentSize
計算出collectionView的contentSize(遍歷MaxY數(shù)組找出最長列即可得到新的高度)。詳細代碼如下@interface RealityShowLayout ()/** 這個字典用來存儲每一列最大的Y值(每一列的高度) */@property (nonatomic, strong) NSMutableDictionary *maxYDict;/** 存放所有的布局屬性 */@property(nonatomic,strong)NSMutableArray *attributeArray;
@end@implementation RealityShowLayout
- (NSMutableDictionary *)maxYDict {? ? ?
? ? ?if (!_maxYDict) {? ?
? ? ? ? ? ? ?self.maxYDict = [[NSMutableDictionary alloc] init];
}?
return _maxYDict;
}
- (NSMutableArray *)attributeArray {
? if (!_attributeArray) {?
? ? ? self.attributeArray = [[NSMutableArray alloc] init];?
}
? ?return _attributeArray;
}
#pragma mark -初始化默認值
- (instancetype)init {
? ?if (self = [super init]) {
? ? ? ? self.columnMargin = 10;
? ? ? ? self.rowMargin = 10;?
? ? ? ?self.columnsCount = 2;?
? ? ? self.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
}
? ? ?return self;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{ return YES;}
- (void)prepareLayout {
? [super prepareLayout];? // 1.清空最大的Y值?
? ? for (int i = 0; i<2; i++) {
? ? ?NSString *column = [NSString stringWithFormat:@"%d", I]; ? ? ? ? ? ?self.maxYDict[column] = @(self.sectionInset.top);?
}
[self.attributeArray removeAllObjects]; // 總 item 數(shù)
? NSInteger count = [self.collectionView numberOfItemsInSection:0];
? ?for (int i = 0; i *)layoutAttributesForElementsInRect:(CGRect)rect {? ?
? ? return self.attributeArray;
}
// 計算ContentSize- (CGSize)collectionViewContentSize {? ? // 默認最大Y值在第0列? ? __block NSString *maxColumn = @"0";? ?
?[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {? ? ? ?
? ?if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {?????????
? ? ? ? ? ? ? ?maxColumn = column;? ? ? ?
}? ? }];? ?
return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {? ? // 1.計算尺寸? ? CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin) / self.columnsCount;? ? // 代理計算傳入高的值? ? CGFloat height = [self.delegate flowLayout:self heightForWidth:width atIndexPath:indexPath];? ? // 2.0假設最短的那一列的第0列? ? __block NSString *minColumn = @"0";? ? // 遍歷字典找出最短的那一列? ? [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {? ? ? ? if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {? ? ? ? ? ? ? ? minColumn = column;? ? ? ? }? ? }];? ? // 2.1計算位置? ? ? ? CGFloat x = self.sectionInset.left + (self.columnMargin + width) * [minColumn intValue];? ? ? ? CGFloat y = [self.maxYDict[minColumn] floatValue]+ _rowMargin;? ? ? ? self.maxYDict[minColumn] = @(y + height);? ? ? ? // 3.創(chuàng)建屬性? ? ? ? UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];? ? ? ? attrs.frame = CGRectMake(x, y, width, height);? ? ? return attrs;}橫向瀑布流原理一樣........此處省略一萬字
3.進度條
https://github.com/AliThink/HorizontalProgress